Before adding a node
Clicking on "First" in the graph will trigger the Click event, and the effect
After clicking the button, after adding the node
Clicking on "First" in the graph does not trigger a click event.
The code looks like this:
<!DOCTYPE HTML><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><HTML><Head><Scriptsrc= "Https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></Script><Script> $(function(){ $(". A1"). On ("Click",function() {alert ("trigger A-tag click event! "); }); }); functioncl () {$ (". Div2"). HTML ('<a class= "A1" > First </a><br><a class= "A2" > Second </a>'); }</Script></Head><Body> <Divclass= "Div1"> <Divclass= "Div2"> <aclass= "A1">First one</a> </Div> <inputonclick= "cl ()"type= "button"value= "Add Node"/> </Div></Body></HTML>
The above problems can be solved by binding several methods
1. Add an onclick event directly when adding a tag;
2. The effect of activating the Click event is achieved by binding the parent class or body of this tag.
The following code is changed:
<!DOCTYPE HTML><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><HTML><Head><Scriptsrc= "Https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></Script><Script> $(function(){ /*$ (". A1"). On ("click", Function () {alert ("trigger A-tag click event!"). "); });*/ /*$ (". Div1"). On ("click", ". A1", function () {alert ("trigger A-tag click event!"). "); });*/ $(". Div2"). On ("Click",". A1",function() {alert ("trigger A-tag click event! "); }); /*$ ("body"). On ("click", ". A1", function () {alert ("trigger A-tag click event!"). "); });*/ }); functioncl () {$ (". Div2"). HTML ('<a class= "A1" > First </a><br><a class= "A2" > Second </a>'); }</Script></Head><Body> <Divclass= "Div1"> <Divclass= "Div2"> <aclass= "A1">First one</a> </Div> <inputonclick= "cl ()"type= "button"value= "Add Node"/> </Div></Body></HTML>
If it appears, click Once, two times the Click event appears,
Consider using the propagation () method to handle bubbling.
jquery dynamically added node event failed to fire