jquery gives a way to dynamically add elements that bind events

Source: Internet
Author: User

Binding events in jquery typically use BIND, or click, but this can only be defined for elements that have already been loaded, and those that later add inserts need to be bound separately. Use live before the 1.7 release. However, it is recommended to use on after version 1.8. This article describes how to bind events to dynamically added elements in jquery
In real-world development, you'll encounter situations where you want to bind a dynamically generated HTML element to a trigger event

For example
?

1 2 3 < div id = "Testdiv" > < ul ></ ul > </ div >

Need to add a click event to the <ul> dynamically added <li> tag

jquery version 1.7 previously used live dynamic binding events?

1 2 $ ("#testdiv ul Li"). Live ("Click", Function () {});

jquery version 1.7 uses on dynamic bind event after

Testdiv ul page already loaded elements

Li page dynamically generated elements JS write
?

1 2 3 $ ("#testdiv ul"). On ("Click", "Li", function () {//do something here});

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.