简体   繁体   中英

Hammer.js with the JQuery plugin not firing event

I have the following JS for a doubletap event on the element '.snap_img' with the Hammer.js JQuery plugin.

$("body").hammer().on('doubletap','.snap_img',function() {
    img_src = (this).getAttribute('src');
    alert(img_src);
    return false;
});

The element '.snap_img' is created dynamically by an AJAX post() request. Therefore, I need event delegation to be able to fire the Hammer event. This JS is based on this piece of code which successfully fires a click event on the dynamically loaded .snap_img elements:

$("body").on('click','.snap_img',function() {
    img_src = (this).getAttribute('src');
    alert(img_src);
    return false;
});

How do I make the code with the Hammer JQuery plugin work? I suppose the solution should be very similar to the second piece of code in this question, but I can't seem to figure it out.

If you are using Hammer , you can try something like this:

var i = 0
$('.snap_img').get(0).hammer().on('doubletap', function(e) {
  i++
  console.log( 'double tap' + i )
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM