我试图阻止对链接和项目的多次点击,这会导致问题.
我使用jQuery将点击事件绑定到按钮(jQuery UI)和图像链接(<a><img /></a>
).
有没有办法做到 - 一次为所有人阻止其他事件在点击发生后触发?
或者我是否必须维护一个名为_isProcessing的全局变量,并为每个事件处理程序将其设置为true?
谢谢
编辑:(澄清)感谢您的回答,我的问题不是阻止事件的冒泡,而是防止多个并发点击.
use*_*716 32
有多种方法可以防止并发点击运行代码.
一种方法是unbind('click')
在元素上,然后.bind()
在你准备好时再次使用它.
我宁愿使用某种旗帜.它可能是一个变量,但我宁愿为这个元素分配一个类,比如.processing
完成后删除它.因此,您可以让处理程序检查该类是否存在,以确定应运行的代码.
$('someElement').click(function() {
var $th = $(this);
if($th.hasClass('processing'))
return;
$th.addClass('processing');
// normal code to run
// then when done remove the class
$th.removeClass('processing');
});
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用元素.data()
来设置类似的标志,$(this).data('processing', true);
然后在完成时将其设置为false.
Teh*_*One 22
如上所述,有event.preventDefault,event.stopPropagation并返回false.
$("a").click(function(e) {
e.preventDefault();
e.stopPropagation();
return false;
}
Run Code Online (Sandbox Code Playgroud)
你看过preventDefault吗?
$("a").click(function(e) {
e.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)
你也可以尝试stopImmediatePropagation()
或stopPropagation()
你也可以调查这个one()
事件.
将处理程序附加到元素的事件.每个元素最多执行一次处理程序.
归档时间: |
|
查看次数: |
68103 次 |
最近记录: |