英文:
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上测试过这一点。)是否有另一个事件会在完整的右键单击发生时触发(既按下又释放)?或者我需要使用onmousedown
和onmouseup
来自己进行所有检查吗?
(另外,以防有人指出——是的,并非每个人都有“右键”。但这个功能并不绝对必要,因此该产品应该可以在触摸屏上使用。)
谢谢。
英文:
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("mousedown", function(event){
lastElement = event.target;
});
document.addEventListener("mouseup", function(event){
// Determine which button was clicked
let button = null;
switch (event.button){
case 0:
button = "left";
break;
case 1:
button = "middle";
break;
case 2:
button = "right";
break;
}
console.log("You clicked and released the " + button + " button.");
console.log("You were on the " + lastElement + " when you clicked the mouse and you were on the " + event.target + " element when you released the mouse.");
});
<!-- language: lang-html -->
<p>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.</p>
<section>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.</section>
<br>
<div>Item One</div>
<h1>Item Two</h1>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论