jquery dynamic add element failed to trigger binding event

Source: Internet
Author: User

After adding elements dynamically with jquery, it is not possible to trigger events for dynamically added elements. The solution is as follows:

Method One: Bind the Live event (the live event is only supported under jquery1.9, not supported in the high version).

123 $(".newBtn").live("click",function(){ ///jquery 1.9(不包括1.9)以下可以               alert(‘这里是动态元素添加的事件‘);           })

method Two: Take advantage of the on () event binding ($ (Parentele). On ("click", ". Thisele", function () {})

123 $ ("Body"). On ("click", ". Newbtn", function () {                 alert ( ' Here is an event added by dynamic Element '); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; br >// The Parentele here is Thisele's father or ancestor element, Parentele can be document, body, etc. < br >< BR >< BR >//Note: If the function called at this time is an externally defined function, do not add () at the time of the call, or skip the click event directly triggering the function
  $ ("Body"). On ("click", ". Newbtn", AA);
function aa () {
Alert (' Here is the event of dynamic element addition ');
}

jquery dynamic add element failed to trigger binding event

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.