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: