js滑鼠,鍵盤,座標軸事件

來源:互聯網
上載者:User

標籤:

滑鼠按下事件,左鍵是0,滑輪是1,右鍵2

     document.getElementById("box").onmousedown =function(e)        {            if (e.button==0)            {                this.style.background="#c00";            }            else if(e.button==1)            {               this.style.background="#0c0";            }            else if(e.button==2)            {              this.style.background="#00c";            }            else            {                alert("你按下了"+e.button+"號鍵");            }             console.log(e.type);             console.log(e.screenX);             console.log(e.clientX);             console.log(e.offsetX);}

鍵盤ctrl,alt

        document.onkeydown=function(e)        {            if (e.ctrlKey)            {                alert("你按下了ctrl鍵");            }             else if (e.altKey)            {                alert("你按下了alt鍵");            }            else  if (e.shiftKey)            {                alert("你按下了shift鍵");            }             else if (e.ctrlKey &&altKey &&shiftKey)            {                alert("你按下了三個鍵");            }             console.log(e.type);        }

座標軸時間

var textbox=document.getElementById("textbox");   var val=‘‘;   document.getElementById("box").onmousedown=function(e)   {    e=window.event || e;    val="e.screenX: "+e.screenX+       "\ne.screenY: "+e.screenY+        "\nclientX: "+e.clientX+    //clientX支援所有瀏覽器         "\nclientY: "+e.clientY+   //          "\ne.X: "+e.X+           "\ne.Y: "+e.Y+            "\npageX: "+e.pageX+             "\npageY: "+e.pageY+              "\noffsetX: "+e.offsetX+               "\noffsetY: "+e.offsetY+               "\noffsetX: "+(e.clientX-this.offsetLeft);                    textbox.value=val;                    //offsetFirefox不支援                    //可以用此方法                    //e.clientX-offsetleft                }

//html代碼

<div id="box"> </div>
<textarea id="textbox" style="display:block;margin:50px auto;width:400px;height:200px;"></textarea>


 

js滑鼠,鍵盤,座標軸事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.