jQuery jquery中mousedown和click的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19109754/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:12:06  来源:igfitidea点击:

Difference between mousedown and click in jquery

jquery

提问by poorvank

I am learning events in jquery. While implementing them i came across a doubt. What is the difference between mousedown() and click() event. And which event should i use at what condition.?

我正在学习 jquery 中的事件。在实施它们时,我遇到了一个疑问。mousedown() 和 click() 事件有什么区别。我应该在什么条件下使用哪个事件。?

For example: Both the events perform the same task in the below code:

例如:这两个事件在下面的代码中执行相同的任务:

$("#p1").mousedown(function(){
  alert("Mouse down over p1!");
});


$("#p1").click(function(){
  alert("Mouse down over p1!");
});

Both perform the same.Can someone clarify the difference. If same, which should i prefer?.

两者执行相同。有人可以澄清区别。如果相同,我应该更喜欢哪个?

回答by pablofiumara

onMouseDownwill trigger when either the left or right (or middle) is pressed. Similarly, onMouseUpwill trigger when any button is released. onMouseDownwill trigger even when the mouse is clicked on the object then moved off of it, while onMouseUpwill trigger if you click and hold the button elsewhere, then release it above the object.

onMouseDown将在按下左或右(或中间)时触发。同样,onMouseUp释放任何按钮时都会触发。onMouseDown即使鼠标在对象上单击然后从对象上移开onMouseUp也会触发,而如果您在别处单击并按住按钮,然后将其释放到对象上方,则会触发。

onClickwill only trigger when the left mouse button is pressed and released on the same object. In case you care about order, if the same object has all 3 events set, it's onMouseDown, onMouseUp, then onClick. Each even should only trigger once though.

onClick只有在同一对象上按下并释放鼠标左键时才会触发。如果您关心顺序,如果同一个对象设置了所有 3 个事件,则为onMouseDown, onMouseUp,然后onClick。每个甚至应该只触发一次。

Details:

细节:

http://api.jquery.com/click/
http://api.jquery.com/mouseup/
http://api.jquery.com/mousedown/

http://api.jquery.com/click/
http://api.jquery.com/mouseup/
http://api.jquery.com/mousedown/

Sourcewritten by Anton Baksheiev

来源Anton Baksheiev 撰写

回答by Jivings

A mousedownevent is fired when the mouse button is pressed but before it is released.

mousedown当鼠标按钮被按下但在释放之前会触发一个事件。

The clickevent is fired after the mousedownand mouseupevents.

click事件在mousedownmouseup事件之后触发。

回答by Artyom Neustroev

$(element).click()fires, when you pressmouse button and then releaseit.

$(element).click()触发,当您按下鼠标按钮然后松开时。

$(element).mousedown()fires, then you press the mouse button.

$(element).mousedown()触发,然后按下鼠标按钮。

Try to hold the clicked button over that button, and then release it here: http://jsfiddle.net/n9rJ9/

尝试将单击的按钮放在该按钮上,然后在此处释放它:http: //jsfiddle.net/n9rJ9/

回答by yan.kun

They do not. You might think so, as you bound both event handlers on the same element, so mousedown will always fire before the click event will occur.

他们不。您可能会这么认为,因为您将两个事件处理程序绑定在同一个元素上,所以 mousedown 总是会在 click 事件发生之前触发。

If you bind them on different elements, you will see mousdown will always fire on a button press (any mouse button) without a release and click will fire, after you have released the mouse button of the left (primary) side.

如果您将它们绑定在不同的元素上,您会看到鼠标按下按钮(任何鼠标按钮)时总是会触发而无​​需释放,并且在您释放左侧(主要)侧的鼠标按钮后,单击会触发。

See this small jsfiddle: http://jsfiddle.net/wBfbm/

看到这个小jsfiddle:http: //jsfiddle.net/wBfbm/

$("#p1").mousedown(function(){
  alert("Mouse down over p1!");
});


$("#p2").click(function(){
  alert("Mouse down over p1!");
});

回答by Lingasamy Sakthivel

onMouseDownwill trigger when either the left or right (or middle) is pressed.

onMouseDown将在按下左或右(或中间)时触发。

onClickwill only trigger when the left mouse button is pressed and released on the same object.

onClick只有在同一对象上按下并释放鼠标左键时才会触发。

回答by Gaurav Agrawal

Try this way. Because event.stopPropagation does not stop click event event from mouse down. Mouse down and click events are not related to each other.

试试这个方法。因为 event.stopPropagation 不会从鼠标向下停止单击事件事件。鼠标按下和点击事件彼此无关。

 var mousedDownFired = false;

  $("#id").mousedown(function(event){
      mousedDownFired =true;
      //code
  });

  $("#id").click(function(event){
     if(mousedDownFired)
      {
         mousedDownFired = false;
         return;
      }
      //code
 });

Updated:

更新:

NO . Mouse events are triggered like this way

不 。鼠标事件是这样触发的

1) MouseDown

1) 鼠标按下

2) Click

2)点击

3) MouseUp

3) 鼠标向上

if mouse down is triggered then flag will enable after mouse down event click will trigger .In this click event will disable the flag variable. this will work as cyclic way. not going to consider two mouse down or two click

如果触发鼠标按下,则在鼠标按下事件点击后将启用标志。在此点击事件中,将禁用标志变量。这将作为循环方式工作。不会考虑两次鼠标按下或两次点击

回答by user8640104

onmousedown + onmouseup = onclick (click event);

onmousedown + onmouseup = onclick(点击事件);

** actions **                             ** event **

mouse press/down                          onmousedown
mouse release/up                          onmouseup
mouse press/down + mouse release/up       onclick
Key Enter/space press                     onclick