javascript - Question about the event not working after dynamic loading and then binding the event
淡淡烟草味2017-07-05 11:05:56
0
5
1073
After I dynamically created three p boxes with ajax, why does it not work when I bind click events to the boxes? Is it only useful to bind events to him when ajax is created? This binds it three times. Why is this? Is there a better way?
$(document).on('click','#a',function(){ //TODO }); Borrowing the answer from the first floor, I think you can find its parent element first and then bind it
I have encountered this before. Using onclick on dynamically loaded DOM does not work, because it only works on the existing DOM. Use $(document).on('click','#a',function(){} ) will work, you can try it.
For example, you can take a look at the jquery implementation
$(document).on('click','#a',function(){ //TODO }); Borrowing the answer from the first floor, I think you can find its parent element first and then bind it
I have encountered this before. Using onclick on dynamically loaded DOM does not work, because it only works on the existing DOM. Use $(document).on('click','#a',function(){} ) will work, you can try it.
You can use event delegation to achieve this, for example
If you want to add a list (.list) in the wrapper, you can write the delegate like this (simple writing):
You can use JQ’s on method and delegate method. If it’s native, use event delegation