jquery has an invalid problem with dynamically generated element bindings ~ ~

Source: Internet
Author: User

Problem: Many times it is found that binding the Click event to a dynamically generated element is not valid ~

Cause: Binding directly to dynamically generated elements is not valid because jquery scans the document to find all the $ (') elements and binds the function to the click event of each element, indicating that there are elements that exist on the existing page and that the dynamically generated elements are not included .

Workaround: The agent or delegate on (), the implementation principle is the bubbling of events, in the specified ancestor element register event, element event trigger, propagate to this element and then filter.

Events can be bound in ancestor elements, such as a div is an ancestor element, and the newly generated element is a child of the Div, so events of dynamically generated elements can be bound.

  

$ (". Parents"). On ("click", ". Children",function() {   //})// the binding ancestor element. Parents delegate to his child element, which is the element you really want to bind ~ ~

jquery has an invalid problem with dynamically generated element bindings ~ ~

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.