JavaScript常用事件
除了剛才提到的onclick 事件,還有這些常用的事件:
#onclick 點(diǎn)擊
- ##ondblclick 雙擊
- onfocus 元素獲得焦點(diǎn)
- onblur 元素失去焦點(diǎn)
- onmouseover 滑鼠移到某個(gè)元素之上
- onmouseout 滑鼠從某元素移開
- onmousedown 滑鼠按鈕被按下
- onmouseup 滑鼠按鍵被放開
- onkeydown 某個(gè)鍵盤按鍵被按下
- onkeyup 某個(gè)鍵盤按鍵被放開
- #onkeypress 某個(gè)鍵盤按鍵被按下並放開
<html> <head></head> <body> <div style="background-color:green;width:200px;height:50px;margin:20px;padding-top:10px;color:#ffffff;font-weight:bold;font-size:18px;text-align:center;" onmouseover="this.innerHTML='good'" onmouseout="this.innerHTML='you have moved out'" >move your mouse to here</div> </body> </html>滑鼠移入時(shí),顯示“good”,滑鼠移出時(shí)顯示“you have moved out”:
<html> <head> <script> function mDown(obj) // 按下鼠標(biāo) 的 事件處理程序 { obj.style.backgroundColor="#1ec5e5"; obj.innerHTML="release your mouse" } function mUp(obj) // 松開鼠標(biāo) 的 事件處理程序 { obj.style.backgroundColor="green"; obj.innerHTML="press here" } </script> </head> <body> <div style="background-color:green;width:200px;height:35px;margin:20px;padding-top:20px;color:rgb(255,255,255);font-weight:bold;font-size:18px;text-align:center;" onmousedown="mDown(this)" onmouseup="mUp(this)" >press here</div> </body> </html>運(yùn)行結(jié)果可見,按下滑鼠時(shí),顯示“release your mouse”,背景變?yōu)樗{(lán)色;鬆開滑鼠後,顯示為“press here”,背景變?yōu)榫G色。