当用户双击DOM对象(例如按钮和链接等)时,对于用户交互一直是个麻烦的问题。 幸运的是, jQuery 提供了一个相当棒的解决方法。 那就是.one()。
它附加了一个元素事件的处理程序并且每个元素只能运行一次事件处理器函数。
.one( events [, selector ] [, data ], handler(eventObject) )
规定添加到元素的一个或多个事件。由空格分隔多个事件。必须是有效的事件。就像“click”和“keydown.myPlugin”一样。
$("#saveBttn").one("click", function () { alert("This will be displayed only once."); });
或者
$("body").one("click", "#saveBttn", function () { alert("This displays if #saveBttn is the first thing clicked in the body."); });
上述代码关键在于:
$("#saveBttn").on("click", function (event) { alert("This will be displayed only once."); $(this).off(event); });
换句话说这和在绑定事件处理函数中显式调用off()作用是一样的
上面所提到的方法是jQuery 1.7的新特性,所以如果你的元素点击事件不止触发一次,这可能是个解决方案哦。
原文:http://www.cnblogs.com/Rockys/p/3596040.html