在JavaScript中,在右鼠标按钮按下并释放后调用一个函数?

huangapple go评论63阅读模式
英文:

Calling a function after the right mouse button is pressed and released in JavaScript?

问题

我正在使用JavaScript创建一个客户端应用程序。当在某个特定元素上单击左按钮或右按钮时,我需要执行某些功能,而左右按钮的功能会互相取消,因此希望UI具有对称性。

我可以轻松地在左按钮上设置事件:

element.onclick = function(e) { doSomething(); }

这很好,因为它会检测按钮是否在相同元素上同时按下和释放,并且仅在按钮释放时触发。这正是我想要的。但是右按钮更难处理。看起来标准的方法是这样的:

element.oncontextmenu = function(e) { undoSomething(); return false; }

但这的行为不同:事件在按下按钮时发生(而不是释放按钮),并且没有检查是否完整的点击发生在同一个元素上。(我在Firefox和Chromium上测试过这一点。)是否有另一个事件会在完整的右键单击发生时触发(既按下又释放)?或者我需要使用onmousedownonmouseup来自己进行所有检查吗?

(另外,以防有人指出——是的,并非每个人都有“右键”。但这个功能并不绝对必要,因此该产品应该可以在触摸屏上使用。)

谢谢。

英文:

I'm using JavaScript to make a client-side applet. I need certain functionality when the left button is clicked over a certain element, and also when the right button is so clicked. The left and right functions undo each other, so symmetry in the UI is desired.

I can set an event on the left button just fine:

element.onclick = function(e) { doSomething(); }

This is nice, since it detects if the button was both pressed and released over the same element, and only fires when the button's released. That's exactly what I want. But the right button is harder. The standard way to do this appears to be this:

element.oncontextmenu = function(e) { undoSomething(); return false; }

But this behaves differently: the event happens when the button is pressed (not when it's released) and there's no check to make sure that the complete click happened over the same element. (I've tested this on both Firefox and Chromium.) Is there another event that gets fired when a complete right click happens (both down and up)? Or do I need to muck around with onmousedown and onmouseup to do all my own checking?

(Also, before someone points it out--yes, not everyone has a "right button". But that functionality isn't strictly needed, so this product should be workable with touchscreens.)

Thanks.

答案1

得分: 3

使用mouseup事件并在事件处理程序中使用event.button确定按下了哪个按钮。您还可以使用event.target来确定被单击的元素是什么。

而且,请不要使用事件属性来设置事件,而要使用现代的标准方法.addEventListener()

let lastElement = null;

document.addEventListener("mousedown", function(event){
  lastElement = event.target;
});

document.addEventListener("mouseup", function(event){
  // 确定单击了哪个按钮
  let button = null;
  switch (event.button){
    case 0:
      button = "左键";
      break;
    case 1:
      button = "中键";
      break;
    case 2:
      button = "右键";
      break;      
  }

  console.log("您单击并释放了" + button + "按钮。");
  console.log("当您单击鼠标时,您位于" + lastElement + "上,当您释放鼠标时,您位于" + event.target + "元素上。");
});
<p>尝试在元素上左键、中键或右键单击(不释放鼠标),然后拖动到另一个元素上,然后释放鼠标。</p>
<section>如果您在浏览器外部释放按钮,HTMLElement将被注册为您释放按钮时所在的元素。</section>
<br>
<div>项目一</div>
<h1>项目二</h1>
英文:

Use the mouseup event and determine which button was pressed in the event handler with event.button. You can also use the event.target to determine what the element that was clicked was.

AND, don't use event properties to set up events, use the modern, standard approach of .addEventListener().

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let lastElement = null;

document.addEventListener(&quot;mousedown&quot;, function(event){
  lastElement = event.target;
});

document.addEventListener(&quot;mouseup&quot;, function(event){
  // Determine which button was clicked
  let button = null;
  switch (event.button){
    case 0:
      button = &quot;left&quot;;
      break;
    case 1:
      button = &quot;middle&quot;;
      break;
    case 2:
      button = &quot;right&quot;;
      break;      
  }

  console.log(&quot;You clicked and released the &quot; + button + &quot; button.&quot;);
  console.log(&quot;You were on the &quot; + lastElement + &quot; when you clicked the mouse and you were on the &quot; + event.target + &quot; element when you released the mouse.&quot;);
});

<!-- language: lang-html -->

&lt;p&gt;Try left, middle or right-clicking on an element and (without releasing the mouse) drag over to a different element and then release the mouse.&lt;/p&gt;
&lt;section&gt;If you release the button while outside of the browser, the HTMLElement will be registered as the element you were on when you released the button.&lt;/section&gt;
&lt;br&gt;
&lt;div&gt;Item One&lt;/div&gt;
&lt;h1&gt;Item Two&lt;/h1&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月4日 04:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定