单击事件触发后如何删除事件处理程序?

时间:2020-12-20 20:44:00

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

我有一个点击事件,我在我的页面上的div上连线。

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

一旦click事件被触发,我想取消绑定该div上的事件。

How can I do this? Can I unbind it in the click event handler itself?

我怎样才能做到这一点?我可以在点击事件处理程序本身解除绑定吗?

4 个解决方案

#1


Use the "one" function:

使用“one”功能:

$("#only_once").one("click", function() {
alert('this only happens once');
});

#2


Taken from the jQuery documentation found here:

取自这里找到的jQuery文档:

 $("#unbind").click(function () {
      $("#theone").unbind('click', aClick)
                  .text("Does nothing...");
    });

#3


In plain JavaScript:

在纯JavaScript中:

var myDiv = document.getElementById("myDiv");

myDiv.addEventListener('click', clicked, false);

function clicked()
{
    // Process event here...

    myDiv.removeEventListener('click', clicked, false);
}

Steve

#4


There's the unbind function documented here:

这里记录了unbind函数:

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

Fits your example :)

适合你的例子:)

#1


Use the "one" function:

使用“one”功能:

$("#only_once").one("click", function() {
alert('this only happens once');
});

#2


Taken from the jQuery documentation found here:

取自这里找到的jQuery文档:

 $("#unbind").click(function () {
      $("#theone").unbind('click', aClick)
                  .text("Does nothing...");
    });

#3


In plain JavaScript:

在纯JavaScript中:

var myDiv = document.getElementById("myDiv");

myDiv.addEventListener('click', clicked, false);

function clicked()
{
    // Process event here...

    myDiv.removeEventListener('click', clicked, false);
}

Steve

#4


There's the unbind function documented here:

这里记录了unbind函数:

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

Fits your example :)

适合你的例子:)