我在用 firefox,我想判斷鼠標的按鍵。情況是這樣的,我先按下鼠標左鍵并拖動鼠標選擇文字,在左鍵沒有放開的情況下,再點擊右鍵,請問有沒有辦法判斷這情況下是否擊了右鍵?我試了一下,在onmousedown/onmouseup裡,利用 event.button 無法判斷。因為左鍵沒有放開,所以即使按下右鍵,系統也不會產生一個mousedown或mouseup事件。請問有沒有其它辦法?另外,在網上查到,同時按下左右鍵時event.button返回3。但是,迠個”同時按下”是怎樣定義的?先按下左鍵,在不放開的情況下再按右鍵算同時按下嗎?但我試了很多次,即使我同時按下左右鍵,event.button都是0

解决方案 »

  1.   

    <HTML>
    <head>
    <title>sample</title>
    <script language="JavaScript">
    window.onload=function(){
        var div=document.getElementById("div1");
        div.getElementsByTagName("table")[0].onclick=function(){
            document.getElementById("div2").innerHTML+="Left Click<br>";
        }
        div.getElementsByTagName("table")[0].oncontextmenu=function(){
            document.getElementById("div2").innerHTML+="Right Click<br>";
            return false;
        }
    }
    </script>
    </head>
    <body>
    <div id=div1>
    <table border=1><tr><td>1111111111111111</td></tr>
                    <tr><td>2222222222222222</td></tr>
                    <tr><td>3333333333333333</td></tr></table>
    </div>
    <div id=div2></div>
    </body>
    </html>
      

  2.   

    首先,为什么有这样的需求??这种需求应该修改然后,硬是要解决的话,得把onmousedown/onmouseup/onmousemove放在不同的对象上实现下面是代码<html>
    <body><div id="te" onmousedown="A()">
      div
    </div></body><script>function A()
    {
        document.getElementById("te").onmousemove = function (){
              document.onmousedown = Todo;
        };
        document.getElementById("te").onmouseup = function (){
              Clear();
        };
    }function Todo(e)
    {
         e = e || window.event;
         var leftButton = e.srcElement ? 1 : 0;     //因为是左右键同时按,得加上左键的值
         if (e.button == 2 + leftButton) {alert("按了右键!");}
         else  {alert("按了非右键!");}     Clear();
    }function Clear()
    {
         document.onmousedown = null;
         document.getElementById("te").onmousemove =document.getElementById("te").onmouseup = null;
    }
    </script>
    </html>
      

  3.   

    謝謝回复。但這個還是實現不了我想要的效果。我的意思是,如果按著左鍵不放,此時,如果再click一下右鍵,能否偵側到右鍵被click? 我google了很久,似乎這個javascript實現不了
      

  4.   


    试了2#的代码了么???就是你的要求,手上没FF,IE下没问题,不行的话把Todo函数改成这样function Todo()
    {
         e = arguments[0] || window.event;
         var leftButton = e.srcElement ? 1 : 0;
         var rightButton = 2;     //因为是左右键同时按,得加上左键的值
         if (e.button == rightButton + leftButton) {alert("同时按了左右键!");}
         else  {alert("按了非右键!");}     Clear();
    }
      

  5.   

    我想這是 IE 和 FF 的驅別,在IE下,我試了一下,在那個div下,即使按著左鍵再按右鍵,右鍵的mousedown還是會被響應的,這個沒有問題。但在FF下,卻不行,它非要等到別一鍵釋放了才能響應另一鍵。但還是謝謝,起碼學會在 IE 下還能這樣處理