我想向右键单击事件添加事件侦听器,我知道如何处理简单的单击:
document.getElementById("myBtn").addEventListener("mousedown", function(){ });
右键单击事件怎么样?
contextmenu
活动。
仅使用以下命令测试点击类型:
alert("You pressed button: " + event.button)
<!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>
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");
}});