.click () does not start on anchor tag after changing its upload attribute

My thing is that "The system needs to ask the user that" are you going to open the image or upload it.? "using the confirmation window. If the user clicks" okay ", we should not prevent the default action for this anchor tag, let it go, but if the user clicks on cancel, then a specific image must be loaded ...

HTML:

<a href="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b">test</a>

JS:

$('a').click(function (e) {
    if (!$(this).is('[download]')) {
        var cond = confirm('Press ok to view, cancel to save..');
        if (!cond) {
            e.preventDefault();
            $(this).attr('download', 'download').click().removeAttr('download');
        }
    }
});

Demo

Can anyone tell how to achieve this?

+4
source share
5 answers

this.click(); HTMLElement.click() .

$(this).click(); jquery click onclick, .

$('a').click(function (e) {
    if (!$(this).is('[download]')) {
        var cond = confirm('Press ok to view, cancel to save..');
        if (!cond) {
            e.preventDefault();
            $(this).attr('download', 'download');
            this.click()
            $(this).removeAttr('download');
        }
    }
});

DEMO

+1

, $("a")[0].click()

$('a').click(function (e) {
    if (!$(this).is('[download]')) {
        var cond = confirm('Press ok to view, cancel to save..');
        if (!cond) {
            e.preventDefault();
            $(this).attr('download', 'download');
            $("a")[0].click().removeAttr('download');
        }
    }
});

+1
$('a').click(function (e) {
  if (!$(this).is('[download]')) {
  var resp = confirm("Press ok to view, cancel to save..!");
   if (resp == true) {
  // You pressed OK
    e.preventDefault();
    } else {
  //    Pressed Cancel!";
     $(this).attr('download', 'download').click().removeAttr('download');
   }        
  }
});

fiddle

http://jsfiddle.net/sajrashid/94Hra/18/

0

. . . download. .

:

$('a').click(function (e) {
    var cond = confirm('Press ok to view, cancel to save..');
    if (!cond) {
        $(this).attr('download', 'download');
    } else {
        $(this).removeAttr('download');
    }
});
0

you added a name tag to your anchor tag, for example

<a href="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b" name="myevent">test</a>

Now replace your code

$('a').click(function (e) {

With the help of this

$(document).on("click","a[name='myevent']", function (e) {
-2
source

Source: https://habr.com/ru/post/1547143/


All Articles