JS中一些常用的事件(筆記)

來源:互聯網
上載者:User

標籤:style   class   blog   code   ext   color   

window.onload事件:當文檔和其所有外部資源(片)完全載入並顯示給使用者時就會觸發它。

window.onload = function (){    //當載入完當前頁面和其所有外部資源(片)後,執行這個函數}

 

window.onunload事件:當使用者離開當前頁面時會觸發該事件

window.onunload = function (){    //離開該頁面時執行該函數}

 

event對象:該對象代表了當前事件的狀態,並且只有在事件發生的過程中才生效。對象中存放的是鍵盤按鍵的狀態、滑鼠的位置、滑鼠按鍵的狀態等等。

click事件:滑鼠左按鈕點擊事件。

var tag = document.getElementById(‘IDname‘);tag.onclick = function (event){    //單擊tag元素就執行該函數}

 

contextmenu事件:滑鼠右按鈕點擊事件

//取消滑鼠右鍵var bodyEle = document.getElementsByTagName(‘body‘)[0];bodyEle.oncontextmenu=‘return false‘

 

dblclick事件:滑鼠左按鈕雙擊事件

var tag = document.getElementById(‘IDname‘);tag.ondblclick= function (event){    //雙擊tag元素就執行該函數}

 

mousedown事件:按下滑鼠左按鈕時觸發的事件

var tag = document.getElementById(‘IDname‘);tag.onmousedown = function (){    //在tag元素上按下滑鼠左鍵時就執行該函數}

 

mouseup事件:釋放滑鼠左按鈕時觸發的事件

var tag = document.getElementById(‘IDname‘);tag.onmouseup = function (){    //在tag元素上釋放滑鼠左鍵時就執行該函數}

 

mousemove事件:移動滑鼠時觸發的事件

var tag = document.getElementById(‘IDname‘);tag.onmousemove = function (){    //在tag元素上移動滑鼠時就執行該函數}

 

mouseover事件:當滑鼠進入某個元素範圍時觸發

var tag = document.getElementById(‘IDname‘);tag.onmouseover = function (){    //當滑鼠移入tag元素上時就執行該函數}

 

mouseout事件:當滑鼠移出某個元素範圍時觸發

var tag = document.getElementById(‘IDname‘);tag.onmouseout = function (){    //當滑鼠移出tag元素上時就執行該函數}

 

keyup事件:鍵盤按鍵被鬆開時觸發

//tag元素一般是一個input元素,輸入框var tag = document.getElementById(‘IDname‘);tag.onkeyup = function (){    //鍵盤按鍵被鬆開時執行函數}

 

keydown事件:鍵盤按鍵被按下時觸發

//tag元素一般是一個input元素,輸入框var tag = document.getElementById(‘IDname‘);tag.onkeydown = function (){    //鍵盤按鍵被按下時執行函數}

 

keypress事件:鍵盤按鍵被按下並鬆開時觸發

//tag元素一般是一個input元素,輸入框var tag = document.getElementById(‘IDname‘);tag.onkeypress = function (){    //鍵盤按鍵被按下並鬆開時執行函數}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.