我想在右键事件上添加一个事件监听器,我知道如何处理简单的单击事件:
document.getElementById("myBtn").addEventListener("mousedown", function(){ });
那么右键点击事件呢?
我想在右键事件上添加一个事件监听器,我知道如何处理简单的单击事件:
document.getElementById("myBtn").addEventListener("mousedown", function(){ });
那么右键点击事件呢?
只是测试点击类型,使用:
alert("您按下的按钮是:" + event.button)
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;
}
}
<!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>
mousedown
事件而不是click
事件。请改为监听click
事件而不是mousedown
事件! - Ram