为右键单击事件添加EventListener

问题描述 投票:0回答:4

我想向右键单击事件添加事件侦听器,我知道如何处理简单的单击:

document.getElementById("myBtn").addEventListener("mousedown", function(){ });

右键单击事件怎么样?

javascript prototypejs right-click
4个回答
7
投票

收听

contextmenu
活动。


1
投票

仅使用以下命令测试点击类型:

alert("You pressed button: " + event.button)


0
投票
<!DOCTYPE html>
<html>
<head>
<style>
div { background: yellow; border: 1px solid black; padding: 10px; } 
div {
    background: yellow;
    border: 1px solid black;
    padding: 10px;
}
</style>
</head>
<body>
<div oncontextmenu="myFunction(event)">
<p>Right-click inside this box to see the context menu!
</div>
<script>
function myFunction(e) {
  e.preventDefault();
  //do something differant context menu
  alert("You right-clicked inside the div!");
}
</script>
<!--if it helpful visit once http://topprojects.ml for more stuf-->
</body>
</html>

0
投票
document.body.addEventListener("mousedown", event => {
if (event.button == 0) { // left click for mouse
    alert("left click");
} else if (event.button == 1) { // right click for mouse
    alert("wheel click");
} else if (event.button == 2){   // other buttons on the mouse
    alert("right click");
}});
© www.soinside.com 2019 - 2024. All rights reserved.