jQuery: triggers do not fire on elements loaded with .load()

前端 未结 2 570
别跟我提以往
别跟我提以往 2021-01-21 18:55

I have index.php and use jQuery .load() to load content from load.php.

When I trigger an event on elements that are in the index.php, the event fires. On the same elemen

相关标签:
2条回答
  • 2021-01-21 19:15

    Using click will create an event listener on the element at the time it is called. if the element does not exist at the time click is called the listener is not created. You need to use event delegation using on (or delegate in older versions of jquery).

    Have a look at the on documentation and read about delegation. You'll need something like this:

    $("<selector of static ancestor>").on("click", "#clickme" , function(){})
    
    0 讨论(0)
  • 2021-01-21 19:17

    The click event binder does not bind to any elements created after execution. You can add a click listener immediately following the .load() method, or you can use something like live or delegate to bind to element patterns in the DOM.

    0 讨论(0)
提交回复
热议问题