Ajax Dynamic Refresh element js event failed, ajax dynamic element js

Source: Internet
Author: User

Ajax Dynamic Refresh element js event failed, ajax dynamic element js

When I encountered a situation in my work, after dynamically refreshing a region through Ajax, The js events originally bound to these dom elements became invalid and I did not know the reason for locating them for a long time, later, I checked the information and found that the region Dom element after dynamic refreshing is re-generated, so the js elements before the page are not dynamically bound to the newly generated element. How can this problem be solved, the event Delegate mechanism of jQuery is used here. The sample code is as follows:

Before jQuery1.7, you can use the live event delegation method:

$ (Selector). live ("click", function () {// Event Logic });

JQuery abolished the live event after version 1.7, but adopted a unified api method, which is good news for program developers and does not need to remember so many APIs, unified on call:

$ (Document). on ("click", selector, function () {// Event Logic })
We recommend that you use the on Method in the new version. document tries its best to bind the smallest element of the event, such as the parent element. This will provide better performance. selector is the selector to bind the element.

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.