在JQuery中只触发一个事件

Iva*_*van 2 jquery

你如何让JQuery只触发你设置的最后一个事件处理程序?例如,在此测试用例中,我只想启动最后一个警报("world").

gio*_*_13 5

您可以解除绑定以前的事件处理程序并仅附加所需的事件处理程序.

element.unbind(event).bind(event,eventHandler);
Run Code Online (Sandbox Code Playgroud)


Gal*_*Gal 5

这是一种做法

$("#test").click(function() { alert('hello'); return false; });
// Before the last event handler.
$("#test").unbind("click");
$("#test").click(function() { alert('world'); return false; });
Run Code Online (Sandbox Code Playgroud)