jquery dynamically added node event failed to fire

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.