Bind event (bind () and remove event (unbind () in JQuery ())

Source: Internet
Author: User

Bind event (bind () and remove event (unbind () in JQuery ())

This article describes in detail how to use and share examples of jQuery binding events and removing events. We recommend this article for your reference.

 

 

Sometimes, when an event is executed, you can solve it in a certain way if you want to cancel the event. For example, bind () (bind event) and unbind () (remove the event added through the bind () method) Methods to remove the effect of the event.

For example, in the following case:

 

The Code is as follows:


<Script type = "text/javascript">
$ (Function (){
$ ('# Btn'). bind ("click", function (){
$ ('# Test'). append ("<p> bind function 1 </p> ");
}). Bind ("click", function (){
$ ('# Test'). append ("<p> bind function 2 </p> ");
}). Bind ("click", function (){
$ ('# Test'). append ("<p> bind function 3 </p> ");
});
})
</Script>

 

Html section:

 

The Code is as follows:


<Body>
<Button id = "btn"> Click Me </button>
<Div id = "test"> </div>
</Body>

 

When the button btn is clicked, three click events are triggered. The append () method here transmits the content of three paragraphs to the div layer.

The append () method appends the specified content at the end of the selected element (still inside. It is different from the html () method. The html () method changes the content of the entire element rather than appending content to the end of the element. The text () method is similar to the html () method, but the difference is that the html code can be written in the html () method and can be correctly parsed, while the text () method () only when the html code is a normal string.

Every time you click here, an event is executed and you want to add a paragraph at the end of the div layer. The following code cancels the event effect. You can delete the event to invalidate the click effect:

 

The Code is as follows:


<Script type = "text/javascript">
$ (Function (){
$ ('# Btn'). bind ("click", function (){
$ ('# Test'). append ("<p> bind function 1 </p> ");
}). Bind ("click", function (){
$ ('# Test'). append ("<p> bind function 2 </p> ");
}). Bind ("click", function (){
$ ('# Test'). append ("<p> bind function 3 </p> ");
});
$ ('# Delall'). click (function (){
$ ('# Btn'). unbind ("click ");
});
})
</Script>

 

$ ('# Btn'). unbind ("click"); this code cancels the click event under Element btn. It is not only effective for the bind () method, but also for the click () method. In a sense, bind ("click", function () {}) and click (function () {}) are equivalent.

You can also delete specific events based on specific methods. The following code can be referenced:

 

The Code is as follows:


<Script type = "text/javascript">
$ (Function (){
$ ('# Btn'). bind ("click", myFun1 = function (){
$ ('# Test'). append ("<p> bind function 1 </p> ");
}). Bind ("click", myFun2 = function (){
$ ('# Test'). append ("<p> bind function 2 </p> ");
}). Bind ("click", myFun3 = function (){
$ ('# Test'). append ("<p> bind function 3 </p> ");
});
$ ('# Deltwo'). click (function (){
$ ('# Btn'). unbind ("click", myFun2 );
});
})
</Script>

 

The second parameter of the unbind () method is the name of the execution function corresponding to the event. After the execution, only the myFun2 event is deleted, and the other two click events are executed normally.

Another method similar to the bind () method is one (). The difference is that the one () method is executed only once. Bind a one-time event handler function for a specific event (such as click) Matching Element. The Code is as follows:

 

The Code is as follows:


<Script type = "text/javascript">
$ (Function (){
$ ('# Btn'). one ("click", function (){
$ ('# Test'). append ("<p> bind function 1 </p> ");
}). One ("click", function (){
$ ('# Test'). append ("<p> bind function 2 </p> ");
}). One ("click", function (){
$ ('# Test'). append ("<p> bind function 3 </p> ");
});
})
</Script>

 

Click it and run it only once. Clicking again will not trigger the effect. This is the one method.

The above is all the content of this article. I hope this article will give you a better understanding of jQuery's binding events and removal events.

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.