addEventListener到右键单击事件

时间:2015-08-21 18:28:17

标签: javascript prototypejs right-click

我想在右键单击事件中添加事件监听器,我知道如何处理简单的点击: document.getElementById("myBtn").addEventListener("mousedown", function(){ });

右键点击事件怎么样?

3 个答案:

答案 0 :(得分:5)

收听contextmenu事件。

答案 1 :(得分:1)

只需使用以下方法测试点击类型: alert("You pressed button: " + event.button)

答案 2 :(得分: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>