How can I remove the event handler once the click event has fired?

后端 未结 4 1138
北荒
北荒 2020-12-12 07:13

I have a click event I wired up on a div on my page.

Once the click event has fired, I want to unbind the event on that

相关标签:
4条回答
  • 2020-12-12 07:40

    There's the unbind function documented here:

    http://docs.jquery.com/Events/unbind

    Fits your example :)

    0 讨论(0)
  • 2020-12-12 07:45

    Use the "one" function:

    $("#only_once").one("click", function() {
    alert('this only happens once');
    });
    
    0 讨论(0)
  • 2020-12-12 07:47

    Taken from the jQuery documentation found here:

     $("#unbind").click(function () {
          $("#theone").unbind('click', aClick)
                      .text("Does nothing...");
        });
    
    0 讨论(0)
  • 2020-12-12 08:01

    In plain JavaScript:

    var myDiv = document.getElementById("myDiv");
    
    myDiv.addEventListener('click', clicked, false);
    
    function clicked()
    {
        // Process event here...
    
        myDiv.removeEventListener('click', clicked, false);
    }
    

    Steve

    0 讨论(0)
提交回复
热议问题