js常用對象:點擊、雙擊、onload事件、滑鼠相關、onblur事件和onfocus事件等;通過迴圈給多個元素添加事件、通過addEventListener() 方法 監聽事件函數

來源:互聯網
上載者:User

標籤:onfocus事件   get   添加   idt   通過   onscroll   down   cli   sem   

1、通過標籤內部添加事件

(1)onclick事件:單擊事件

(2)ondbclick事件:雙擊事件

(3)onload事件:只能在body中使用,入口函數裡有:window.   。onload=function(  ){  }

(4)onchange事件:改變事件

(5)onblur事件和onfocus事件:失去焦點和聚焦

(6)onscroll事件:滾動倫

(7)滑鼠相關事件:

----------------------onmouseover移到對象上方是就觸發事件,同一地區無論怎麼移動就只觸發一次

----------------------onmouseout滑鼠離開對象觸發事件調用函數

----------------------onmousemove移到對象上方是就處罰事件調用函數同一地區滑鼠移動一次就觸發一次,一般不常用

----------------------onmouseup滑鼠抬起觸發事件

----------------------onmousedown滑鼠按下觸發事件電泳函數

舉例說明:

<body>
<div id="one" onclick="dianji()">點擊事件</div>
<div id="one" ondblclick="shuangji()">雙擊事件</div>
改變事件:<input type="text" id="" name="" value="" placeholder="請輸入帳號" onchange="change()"/><br>
失去焦點:<input type="text" id="" name="" value="" placeholder="失去焦點" onblur="sqjd()"/><br>
獲得焦點:<input type="text" id="" name="" value="" placeholder="獲得焦點" onfocus="hdjd()"/><br>
<div id="mouse" class="mouse1" onmouseover="over()">滑鼠移動到某物件範圍的上方時,觸發事件調用函數。注意:在同一個地區中,無論怎樣移動都只觸發一次函數。</div>
<div id="mouse" class="mouse2" onmouseout="out()">滑鼠離開某物件範圍時,觸發事件調用函數。</div>
<div id="mouse" class="mouse3" onmousemove="move()">滑鼠移動到某物件範圍的上方時,觸發事件調用函數。注意:在同一個地區中,只要滑鼠動一次就觸發一次事件。</div>
<div id="mouse" class="mouse4" onmousedown="down()">當滑鼠按下鍵時觸發事件</div>
<div id="mouse" class="mouse5" onmouseup="up()">當滑鼠鬆開時觸發事件</div>

 

 

2、通過for迴圈添加事件

<div class="nav"><!--通過迴圈給多個元素添加事件-->
<div class="na">首頁</div>
<div class="na">概況</div>
<div class="na">師資</div>
<div class="na">交流</div>
<div class="na">訪客</div>

<script type="text/javascript">
window.onload=function () {
}

//通過迴圈給多個元素添加事件
var nam=document.getElementsByClassName("na");
for(var i=0;i<nam.length;i++){
nam[i].onmouseover=function(){
this.style.backgroundColor="red";
this.style.color="white";
}//滑鼠上去背景由棕色變成紅色,字型由黑色變成白色
nam[i].onmouseout=function(){
this.style.backgroundColor="blue";
this.style.color="black";
}
}

</script>

3、通過addEventListener() 方法 監聽事件函數

<body>

<button id="bt">添加監聽事件點我</button><br><br><!--通過addEvenListener()方法 監聽事件函數-->

</body>

<script type="text/javascript">
window.onload=function () {
}

var btm=document.getElementById("bt");
btm.addEventListener("click",aa);
function aa(){
alert("我是監聽點擊事件")
}

</script>

 

js常用對象:點擊、雙擊、onload事件、滑鼠相關、onblur事件和onfocus事件等;通過迴圈給多個元素添加事件、通過addEventListener() 方法 監聽事件函數

聯繫我們

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