$('.ajax').click
(
function()
{
// If been bound then we need to return here.
alert(':D');
}
)
$('.ajax').click
(
function()
{
// If been bound then we need to return here.
alert(':D');
}
)
在这种情况下,我调用了重复的代码。如何检测事件是否已绑定(bind)以防止其触发两个警报框?
最佳答案
在 jQuery 中有一个非常好的方法可以做到这一点。
这是一个例子。
function alertEvent() {
alert(":D");
}
$(".ajax").bind("click", alertEvent);
//When you want to ensure it won't happen twice...
$(".ajax").unbind("click", alertEvent);
$(".ajax").bind("click", alertEvent);
此方法只会删除您指定的事件,这使其非常适合您想要执行的操作。
关于jquery - 如何防止事件被多次绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/968949/