觸發(fā)
mousedown時,e.button的值可以區(qū)別點(diǎn)擊鼠標(biāo)的哪個鍵。0是左鍵,1是滾輪,2是右鍵。
要自定義右鍵菜單時,要先去掉默認(rèn)的contextmenu事件
<div class="right">
在此區(qū)域點(diǎn)擊右鍵
</div>
<script>
window.onload = function(){
var right = document.querySelector('.right');
//去掉默認(rèn)的contextmenu事件,否則會和右鍵事件同時出現(xiàn)。
right.oncontextmenu = function(e){
e.preventDefault();
};
right.onmousedown = function(e){
if(e.button ==2){
alert("你點(diǎn)了右鍵");
}else if(e.button ==0){
alert("你點(diǎn)了左鍵");
}else if(e.button ==1){
alert("你點(diǎn)了滾輪");
}
}
}
</script>