03-DOM事件
03-DOM事件
分為鼠標(biāo)事件和鍵盤事件
鼠標(biāo)事件:
html文件:
<h1 id="h1">hello world</h1><button onclick="fun()">click</button><button>click2</button><button class="btn2">dblclick</button><button class="btn3">按下/松開</button><button class="btn4">移入/移出</button><!-- 鍵盤事件 --><div><input type="text" class="input" /></div>js文件:
//單擊 fun(){ //寫在window.onload方法外,不然則不會執(zhí)行,這個涉及到函數(shù)作用域問題document.getElementById('h1').style.color = "red"; };window.onload = function(){//單擊document.getElementsByTagName("button")[1].onclick = function(){};document.getElementsByTagName("button")[1].onclick = fun2 ; //法1document.getElementsByTagName("button")[1].addEventListener("click",fun2); //法2function fun2(){};//雙擊document.querySelector(".btn2").ondblclick =fun2;//按下、松開document.querySelector(".btn3").onmousedown=function(){document.getElementById("h1").innerHTML = "按鈕按下了";document.getElementById("h1").style.color="blue";}document.querySelector(".btn3").onmouseup=function(){document.getElementById("h1").innerHTML = "按鈕松開了";document.getElementById("h1").style.color="red";};//移入、移出//onmouseover onmouseout//onmouseenter onmouseleave// onmusemovedocument.querySelector(".btn4").onmousemove=function(event){document.getElementById("h1").innerHTML = event.clientX; //當(dāng)前鼠標(biāo)移入與X軸的距離}鍵盤事件 按下鍵盤時發(fā)生的狀態(tài)document.querySelector(".input").onkeyup = function(){ //類似vue里面的雙向數(shù)據(jù)綁定document.getElementById("h1").innerHTML = this.value;} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
- 上一篇: 02-DOM选择器
- 下一篇: 04-doucument对象属性和方法