[英]addEventListener to right click event
I would like to add event listener to a right click event, i know how to deal with a simple click: document.getElementById("myBtn").addEventListener("mousedown", function(){ });
我想将事件侦听器添加到右键单击事件,我知道如何处理简单的单击:
document.getElementById("myBtn").addEventListener("mousedown", function(){ });
What about a right click event?右键单击事件呢?
收听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
doSomething;
else if (event.button == 1) { // right click for mouse
doAnotherThing;
else { // other buttons on the mouse
doOtherThings;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.