1、屏蔽浏览器自带的默认弹出上下文菜单的contextmenu事件。
2、使用mouseup或者mousedown事件监控鼠标的右键,就可以形成鼠标右击的事件。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>鼠标右键单击事件</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.model{
width: 100px;
height: 100px;
background: #f00;
display: none;
}
</style>
</head>
<body>
<div id='btn1'>
<span>右键</span>
<div class="model">弹出框</div>
</div>
<script type="text/javascript">
//阻止元素上本身的右键contextmenu事件
document.getElementById('btn1').oncontextmenu=function(){return false}
document.getElementById('btn1').onmouseup=function(e){
if(e.button === 2){
console.log("右键点击");
}
}
</script>
</body>
</html>