addEventListener 右键点击事件
addEventListener to right click event
我想为右键单击事件添加事件侦听器,我知道如何处理简单的单击:
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.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>