Deep understanding of jquery event Removal _jquery

Source: Internet
Author: User

Sometimes the event is done, and the effect of canceling the event can be handled by some means. such as the Bind () method, you can remove the effect of an event by using the Unbind () method.

For example, one of the following cases:

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

HTML section:

<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 inserts the specified content at the end of the selected element (still inside). It is different from the HTML () method, and the HTML () method changes the content in the entire element, rather than inserting the content at 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:

<script type= "Text/javascript" >
  $ (function () {
    $ (' #btn '). Bind ("click", Function () {
           $ (' #test '). Append ("<p> My binding function 1</p>");
       Bind ("click", Function () {
           $ (' #test '). Append ("<p> my Bound function 2</p>");)
       . Bind ("click", Function () {
            $ (' #test '). Append ("<p> My 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:

<script type= "Text/javascript" >
  $ (function () {
    $ (' #btn '). Bind ("click", myFun1 = function () {
           $ (' # Test '). Append ("<p> My binding function 1</p>");
       Bind ("click", myFun2 = function () {
           $ (' #test '). Append ("<p> My binding function 2</p>");)
       . Bind ("click", MyFun3 = function () {
            $ (' #test '). Append ("<p> My 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:

<script type= "Text/javascript" >
  $ (function () {
    $ (' #btn '). One ("click", Function () {
           $ (' #test '). Append ("<p> My binding function 1</p>");
       One ("click", Function () {
           $ (' #test '). Append ("<p> my Bound function 2</p>");)
       . One ("click", Function () {
            $ (' #test '). Append ("<p> My 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 in-depth understanding of jquery event removal is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.