Difference between mousedown and click in jquery

2020-05-14 16:14发布

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.?

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?.

标签: jquery
7条回答
走好不送
2楼-- · 2020-05-14 16:31

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

The click event is fired after the mousedown and mouseup events.

查看更多
迷人小祖宗
3楼-- · 2020-05-14 16:33

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.

 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

2) Click

3) MouseUp

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

查看更多
\"骚年 ilove
4楼-- · 2020-05-14 16:42

$(element).click() fires, when you press mouse button and then release it.

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

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

查看更多
闹够了就滚
5楼-- · 2020-05-14 16:47

onmousedown + onmouseup = onclick (click event);

** actions **                             ** event **

mouse press/down                          onmousedown
mouse release/up                          onmouseup
mouse press/down + mouse release/up       onclick
Key Enter/space press                     onclick  
查看更多
6楼-- · 2020-05-14 16:49

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

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

查看更多
叼着烟拽天下
7楼-- · 2020-05-14 16:57

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

onClick will 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.

Details:

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

Source written by Anton Baksheiev

查看更多
登录 后发表回答