简体   繁体   中英

jQuery mousedown not working in chrome/safari

I'm using this piece of code to wrap a link around a (dynamically) created div, which works good so far in firefox, but not on safari/chrome and ie.

I already know that I should use pointer events, but I'm not sure how to achieve this, since I still need the ".on" event, because of the dynamically created div.

// Create a link around the ID
$(".psv-hud").on('mousedown', '#psv-marker-job1', function() {
    $(this).wrap( "<a href='/psv-marker-job1/'></a>" );
});

Any ideas how I could solve this?

You need to specify an element that is already there when the DOM is created. In the parameters, you specify the elements you want to add the mousedown method. By simply assigning $('.enemy'), it will attach the method to those that are already present in the DOM.

$('body').on('mousedown', '.enemy', function(event) {
    //attack code
}

Jquery on mousedown not working on dynamically generated elements

If this code don't work, .psv-hub isn't exist when mousedown event bind.

First, check change .psv-hub -> document .

$(".psv-hud").on('mousedown', '#psv-marker-job1', function() {
    $(this).wrap( "<a href='/psv-marker-job1/'></a>" );
});

Show me the example page.

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