jquery双击执行单击

时间:2014-01-06 23:43:05

标签: javascript jquery

嗨我有一个像这样的jquery函数

$(".btn").click(function(e){
 alert("hello"); 
});

出于某种原因,它会发出两个警报。这源于我正在使用的插件中的另一个脚本(当我关闭插件时发现)。在我的脚本中,我试图解除绑定,这让我无法修复。我也使用了e.stopPropogation(),但随后杀死了插件函数。有没有办法允许插件函数运行它的路线,然后我的函数只执行一次。

提前致谢。

2 个答案:

答案 0 :(得分:1)

我已经检查了你的小提琴,我发现这个问题是由data-uk-button-radio的div造成的,uikit.min.js绑定了一个带有data-uk-button-radio的div的点击事件,这将触发所有点击事件div的孩子们。

因此,使用event判断此点击操作是来自鼠标还是脚本将解决此问题。

像这样:

(function(){
 $( ".uk-button" ).click(function( event ) {
 if(event.originalEvent)//only event from mouse has this property 
  alert("hello"); 
  // Do something
});
})(jQuery)

fiddle here

答案 1 :(得分:0)

试试这个:基于你的小提琴

$( ".uk-button" ).bind('click.uniqueEvent', function( event) {
    event.stopPropagation();


    alert("hello"); 
   // Do something
});

然后,当你想解开这个特定的事件时。你可以这样做:

unbind(".uniqueEvent")
相关问题