Binding events (Bind ()) and removal events (unbind ()) in jquery _jquery

Source: Internet
Author: User

Sometimes the event is done, and the effect of canceling the event can be handled by some means. For example, bind () (Bind event) and Unbind () (remove events added through the Bind () method) to remove the effect of an event.

For example, one of the following cases:

Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ (' #btn '). Bind ("click", Function () {
$ (' #test '). Append ("<p> binding function 1</p>");
}. Bind ("click", Function () {
$ (' #test '). Append ("<p> binding function 2</p>");
}. Bind ("click", Function () {
$ (' #test '). Append ("<p> binding function 3</p>");
});
})
</script>

HTML section:

Copy Code code 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, where the append () method passes three paragraph contents to the DIV layer.

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

Every time you click here, you will perform an event to add a paragraph at the end of the div layer. The following code cancels the event effect by deleting the event to invalidate the hit effect:

Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ (' #btn '). Bind ("click", Function () {
$ (' #test '). Append ("<p> binding function 1</p>");
}. Bind ("click", Function () {
$ (' #test '). Append ("<p> binding function 2</p>");
}. Bind ("click", Function () {
$ (' #test '). Append ("<p> binding function 3</p>");
});
$ (' #delAll '). Click (function () {
$ (' #btn '). Unbind ("click");
});
})
</script>

$ (' #btn '). Unbind ("click"); the function of this code is to cancel the click event under BTN element. It is not only valid for the bind () method, it is also valid for the Click () method. In some ways, bind ("click", Function () {}) is equivalent to click (function () {}).

You can also delete specific events for specific methods. The following code can refer to:

Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ (' #btn '). Bind ("click", myFun1 = function () {
$ (' #test '). Append ("<p> binding function 1</p>");
). Bind ("click", myFun2 = function () {
$ (' #test '). Append ("<p> binding function 2</p>");
). Bind ("click", MyFun3 = function () {
$ (' #test '). Append ("<p> binding function 3</p>");
});
$ (' #delTwo '). Click (function () {
$ (' #btn '). Unbind ("click", MyFun2);
});
})
</script>

The second parameter of the Unbind () method is the name of the event's due execution function, so that only the MyFun2 event is deleted and the other two click events are executed.

There is also a method one () similar to the Bind () method, the difference being probably that one () method executes only once. Bind a one-time event handler for each matching element's specific event (like click). The code is as follows:

Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ (' #btn '). One ("click", Function () {
$ (' #test '). Append ("<p> binding function 1</p>");
. One ("click", Function () {
$ (' #test '). Append ("<p> binding function 2</p>");
. One ("click", Function () {
$ (' #test '). Append ("<p> binding function 3</p>");
});
})
</script>

Once clicked, it is only performed once. Clicking again does not trigger the effect. This is one method.

The above is the entire contents of this article, I hope this article will enable you to better understand jquery 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.