JavaScript中判断鼠标按键(event.button)

Posted 杨亚男

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript中判断鼠标按键(event.button)相关的知识,希望对你有一定的参考价值。

<div id="test"></div>
<script>
    document.oncontextmenu=function(){
        return false;
    };
    var oDiv=document.getElementById("test");
    oDiv.onmousedown=function(e){
        e= e || window.event;
        var mouseNum = e.button;
this.innerhtml = mouseNum; } </script>

  在线测试:

 

  结论:





event.button
IE
6-8
IE
9+
Chrome FireFox
左键 1 0 0 0
中键 4 1 1 1
右键 2 2 2 2

  我们可以看到右键代码都是2,所以直接判断event.button就可以了

以上是关于JavaScript中判断鼠标按键(event.button)的主要内容,如果未能解决你的问题,请参考以下文章