javascript - jq click event repeated execution problem
曾经蜡笔没有小新2017-07-05 10:38:45
0
9
1010
Help, the elements dynamically generated by jq need to be bound with on to click events to take effect, and the function that executes on also has click events, and then the function is executed twice. How to solve this situation?
The person above made it clearer. 1. Found the problem 1.1 is bound twice, because the dynamically generated element is bound to an event, but in this event the previously bound event is called again
2. Dynamically generated elements do not necessarily need to use dynamic binding events
Event handlers using the delegate() method work on current or future elements (such as new elements created by scripts). Click here to view detailed documents: http://www.w3school.com.cn/jq...
Use the event object to find the target you really want to click
In fact, it is nothing more than the event being bound twice or the event bubbling;
1. Unbind the event and bind it again
2, Cancel bubbling
Remove monitoring first, then monitor
.off(handler).on(handler)
Is this bubbling? e.stopPropagation()
off Unbind first, then bind
The person above made it clearer.
1. Found the problem
1.1 is bound twice, because the dynamically generated element is bound to an event, but in this event the previously bound event is called again
1.2 It is still caused by the bubbling of events (if you are not familiar with bubbling, please read the relevant information first)
2. Dynamically generated elements do not necessarily need to use dynamic binding events
Event handlers using the delegate() method work on current or future elements (such as new elements created by scripts).
Click here to view detailed documents: http://www.w3school.com.cn/jq...
Remember to turn off() after on()