admin管理员组文章数量:1220935
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(){ });
What about a 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(){ });
What about a right click event ?
Share Improve this question edited Aug 21, 2015 at 20:16 Khouloud asked Aug 21, 2015 at 18:28 KhouloudKhouloud 1111 gold badge1 silver badge10 bronze badges4 Answers
Reset to default 9Listen to the contextmenu
event.
Just testing the click type using :
alert("You pressed button: " + event.button)
document.body.addEventListener("mousedown", event => {
if (event.button == 0) { // left click for mouse
alert("left click");
} else if (event.button == 1) { // wheel click for mouse
alert("wheel click");
} else if (event.button == 2){ // right click for mouse
alert("right click");
}});
<!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>
本文标签: javascriptaddEventListener to right click eventStack Overflow
版权声明:本文标题:javascript - addEventListener to right click event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739296775a2156946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论