JQuery binds a click event to an element for multiple executions. jqueryclick

Source: Internet
Author: User

JQuery binds a click event to an element for multiple executions. jqueryclick

Original binding method:

$("#sdfsd").on("click",function(e){ ***** });

This method will only add new methods in the original click method;

Solution: Change the binding method:

$("#sdfsd").unbind("click").click(function(e){ ***** });

Unbind the click method bound to the element before binding the new click Method


The dynamically generated element of jquery automatically executes a click event.

<Script type = "text/javascript" src = "jquery-1.7.2.min.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ (". D"). live ("click", function (){
Alert ("auto click link clicked! ");
});
$ ("# X"). append ("<a class = 'D' href = '#'> click automatically! </A> ");;
$ (". D"). click ();
});
</Script>

<Div id = "x">
</Div>

Pay attention to use live binding. After dynamically generated objects are generated, click

Why does the html element button added by jquery not execute the click Event bound to the class style?

The input and button generated when you add more rows are dynamically generated, so you cannot use click. You need to use live (live is not recommended for Versions later than jquery 1.7.2) or on

Set
$ (". SubmitBtn"). click (function (){
Change
$ (". SubmitBtn"). live ('click', function (){
Or
$ (". SubmitBtn"). on ('click', function (){

Remember, if the elements do not exist during page initialization, but are dynamically generated on the page, perform operations on these elements, such as click, blur, keyup, change ...., use on

. On ('click', function
. On ('blur', function
. On ('keyup', function
....

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.