How to Use jQuery unbind event unbind

Source: Internet
Author: User

Today, I encountered a problem when opening a new window by clicking. After switching the url, I found that window. open is still the original window. I suspect that window. open is cached in the slot?

It seems to be true that I searched it for confusion, and it is useless to try that method.

After troubleshooting various console. log (), I finally suspected that the event was bound. Before binding the Click event, I unbind the previous one as follows:

For example: $ ("p"). unbind ();. This is a good method if you need to unbind all events. If you want to unbind the click event: $ ("p"). unbind ("click"); then OK.

In fact, we usually use $ ("p "). click (function () {}) is also the abbreviation of bind $ ("p "). bind ("click", function (){})

Remove event

An example is as follows:


/Click all events will be removed
$ ("A"). bind ("click. a", function () {alert ("click ");});
$ ("A"). bind ("click", function () {alert ("B ");})
$ ("A"). unbind ("click ");
// The first click event is valid at the first click. After clicking, all click events are removed.
$ ("A"). bind ("click. a", function (){
Alert ("click ");
$ ("A"). unbind ("click ");
});
$ ("A"). bind ("click", function () {alert ("B ")});
// Only the event in click will be removed
$ ("A"). bind ("click. a", function () {alert ("click ");});
$ ("A"). bind ("click", function () {alert ("B ");})
$ ("A"). unbind ("click. ");
// The a event in the click operation is executed once and then removed. Other events are not disturbed.
$ ("A"). bind ("click. a", function (){
Alert ("click ");
$ ("A"). unbind ("click. ");
});
$ ("A"). bind ("click", function () {alert ("B ")});
// The a event in the click operation is executed once and then removed. Other events are not disturbed.
$ ("A"). bind ("click", a = function (){
Alert ("click ");
$ ("A"). unbind ("click", );
});
$ ("A"). bind ("click", function () {alert ("B ")});


Example

<! 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>

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:

 


Bind event


Bind, live, and one also help you to separate some common events, such as the onclick event of the control. When binding an onclick event, you only need:

$ ("# TestButton"). click (function (){
Alert ("I'm Test Button ");
});
It indicates the differences between. bind (),. live () and. delegate () of jQuery.

. Bind ()

$ ('A'). bind ('click', function () {alert ("That tickles! ")});
This is the simplest binding method. The JQuery scan document finds all the $ ('A') elements and binds the alert function to the click event of each element.

. Live ()

$ ('A'). live ('click', function () {alert ("That tickles! ")});
JQuery binds the alert function to the $ (document) element and uses 'click' and 'A' as parameters. Whenever an event bubbles to the document node, it checks whether the event is a click event and whether the target element of the event matches the 'A' CSS selector, if all are used, the function is executed.

The live method can also be bound to a specific element (or "context") rather than a document, like this:

$ ('A', $ ('# INER') [0]). live (...);
. Delegate ()

$ ('# INER'). delegate ('A', 'click', function () {alert ("That tickles! ")});
JQuery scans the document for $ ('# INER') and binds the alert function to $ ('# INER') using the click event and the 'A' CSS selector as parameters. Whenever an event bubbles to $ ('# INER'), it checks whether the event is a click event and whether the target element of the event matches the CCS selector. If both checks are true, it executes the function.

It can be noted that this process is similar to. live (), but it binds the handler to a specific element rather than a document. The savvy JavaScript 'er may come to the conclusion that $ ('A '). live () ==$ (document ). delegate ('A'), is that true? Well, no, not exactly.

Why. delegate () is better than. live ()

For several reasons, people prefer to use jQuery's delegate method instead of the live method. Consider the following example:

$ ('A '). live ('click', function () {blah ()}); // or $ (document ). delegate ('A', 'click', function () {blah ()});

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.