javascript Jquery mousedown 与点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14839440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery mousedown vs click
提问by xhallix
today I discovered something that was quite confusing for me. I just tried to hide s.th via jquery... first I tried to use this
今天我发现了一些令我困惑的事情。我只是试图通过 jquery 隐藏 s.th ......首先我尝试使用它
$(".specificdiv li:nth-child(3)").click(function(){
$(".anotherdiv").hide();
})
....but it does not work.
....但它不起作用。
After a time I tried it this way:
一段时间后,我以这种方式尝试过:
$(".specificdiv li:nth-child(3)").mousedown(function(){
$(".anotherdiv").hide();
})
Can anyone explain me why mousedown works instead of click? Would be great to find out
谁能解释一下为什么 mousedown 起作用而不是单击?很高兴知道
EDIT
编辑
edited the anotherdiv.
编辑了另一个div。
回答by Praveen Kumar Purushothaman
Possible reasons:
可能的原因:
- The event
mousedown
executes beforeclick
, so first come first serve. - The element might have already a
click
event, which prevents this from happening, say that function executes first and it has areturn false
statement in it.
- 事件
mousedown
在之前执行click
,所以先到先得。 - 该元素可能已经有一个
click
事件,这可以防止这种情况发生,假设该函数首先执行并且其中有一个return false
语句。
Now since you are using mousedown
, which is not assigned for this element, it doesn't have any conflicts. This may bea reason, because you didn't post the full code. Feel free to correct. :)
现在,由于您使用的mousedown
是未为此元素分配的 ,因此它没有任何冲突。这可能是一个原因,因为您没有发布完整的代码。随意纠正。:)
On a smaller note, you have $(".anotherdiv").hide();
in the first code and $(".another").hide();
in the second code, missing the div
in the class. Is that a problem?
在较小的说明中,您$(".anotherdiv").hide();
在第一个代码和$(".another").hide();
第二个代码中都缺少div
类中的 。那是问题吗?
回答by walter
Actually the mousedown event is triggered when you push down the button, even you don't let the button up. The click is like the mouseup, when you let the button go up.
实际上,当您按下按钮时会触发 mousedown 事件,即使您不让按钮向上。当您让按钮向上时,点击就像鼠标向上。
In your code you have: ".anotherdiv" and ".another" could it be your error?
在您的代码中,您有:“.anotherdiv”和“.another”可能是您的错误吗?