Getting started with JQuery -- overview and application of unbind method for removing binding events

Source: Internet
Author: User

1. practical operations on DOM objectsIn, since there are bind methods used to bind events, there are also methods used to remove binding events. In JQuery, you can use the unbind method to remove all bound events or an event.

2. Sample Code:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> unbind method </title>
<Script type = "text/javascript" src = "jquery-1.8.3.min.js"> </script>
<Script type = "text/javascript">
$ (Function (){
Function oClick () {// custom event
$ ("# DivTip"). append ("<div> This is the event bound to button 2 </div> ");
};
$ ("Input: eq (0)"). bind ("click", function (){
$ ("# DivTip"). append ("<div> This is an event bound to button 1 </div> ");
});
$ ("Input: eq (1)"). bind ("click", oClick );
$ ("Input: eq (2)"). bind ("click", function (){
$ ("Input"). unbind ("click", oClick );
$ ("# DivTip"). append ("<div> delete button 2 event </div> ");
});
$ ("Input: eq (3)"). bind ("click", function (){
$ ("Input"). unbind ();
$ ("# DivTip"). append ("<div> events bound to remove all buttons </div> ");
});
})
</Script>
</Head>

<Body>
<Div>
<Input id = "button1" class = "btn" value = "button 1" type = "button"/>
<Input id = "button2" type = "button" value = "button 2" class = "btn"/>
<Input id = "button3" type = "button" value = "DELETE button 2 event" class = "btn"/>
<Input id = "button4" type = "button" value = "delete all events" class = "btn"/>
</Div>
<Div id = "divTip"> </div>
</Body>
</Html>

3. Preview:
Click buttons 1 and 2:


Click Delete button 2. Click Delete button 2. No response. Click Delete button 1:


Click Delete all events and then click all buttons:


Related Article

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.