jquery binding Dynamic Elements

Source: Internet
Author: User

Take a small example to illustrate the situation

?
1 2 3 4 5 6 7 8  <script src="jquery-1.11.0.min.js"></script><input type="button" value="点击我" id="k" /> <script>$(‘#k‘).click(function() {    alert("kkkkk");})</script>

If you write this directly on the page, you can pop up "kkkkk". However, when the input element is dynamically loaded and output to the page by Ajax, the popup window is invalidated, which is equivalent to the Click event not being bound to the element with ID K.

This can be written in script:

?
1 2 3 $(document).on(‘click‘,‘#k‘,function() {    alert("kkkkk");})

The usual use of live () or delegate () to replace on () also achieves the desired effect. But note that I use the jquery-1.11.0 version for example, from the beginning of the jquery1.9 version to remove Live (), heard is a performance problem. So the higher version of jquery can be used with delegate () and on ().

jquery binding Dynamic Elements

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.