我的每个 li 都有一个“galleryImage”类,里面是一个带有“delete”类的跨度。当用户点击span delete时,我想让js做点什么,但是下面的代码似乎没有触发,这是怎么回事?
$('.galleryImage .delete').click(function() {
alert("Handler for .click() called.");
});
我的每个 li 都有一个“galleryImage”类,里面是一个带有“delete”类的跨度。当用户点击span delete时,我想让js做点什么,但是下面的代码似乎没有触发,这是怎么回事?
$('.galleryImage .delete').click(function() {
alert("Handler for .click() called.");
});
Try this:
$('body').on('click', '.galleryImage .delete', function() {
alert("Handler for .click() called.");
});
with jQuery > 1.7
Your question doesn't clarify that, those element on which you're trying to add event are dynamic (added to document page after page load) or previously exists in at page load. So I assume that your elements are added to document after page load.
To add events that added to DOM after page load need delegate event which can be done with .on()
in jQuery.
.on()
$(container).on(eventName, target, handlerFunction);