0

我的每个 li 都有一个“galleryImage”类,里面是一个带有“delete”类的跨度。当用户点击span delete时,我想让js做点什么,但是下面的代码似乎没有触发,这是怎么回事?

$('.galleryImage .delete').click(function() {
          alert("Handler for .click() called.");
    });
4

1 回答 1

5

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.


Note

To add events that added to DOM after page load need delegate event which can be done with .on() in jQuery.

Use of .on()

$(container).on(eventName, target, handlerFunction);
于 2012-06-06T16:18:54.177 回答