DOM 事件
HTML DOM 使 JavaScript 有能力對(duì) HTML 事件做出反應(yīng)。
對(duì)事件做出反應(yīng)
我們可以在事件發(fā)生時(shí)執(zhí)行 JavaScript,比如當(dāng)用戶在 HTML 元素上點(diǎn)擊時(shí)。
如需在用戶點(diǎn)擊某個(gè)元素時(shí)執(zhí)行代碼,請(qǐng)向一個(gè) HTML 事件屬性添加 JavaScript 代碼:
HTML 事件的例子:
當(dāng)用戶點(diǎn)擊鼠標(biāo)時(shí)
當(dāng)網(wǎng)頁已加載時(shí)
當(dāng)圖像已加載時(shí)
當(dāng)鼠標(biāo)移動(dòng)到元素上時(shí)
當(dāng)輸入字段被改變時(shí)
當(dāng)提交 HTML 表單時(shí)
當(dāng)用戶觸發(fā)按鍵時(shí)
在本例中,當(dāng)用戶在 <h1> 元素上點(diǎn)擊時(shí),會(huì)改變其內(nèi)容:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <h1 onclick="this.innerHTML='Ooops!'">點(diǎn)擊文本!</h1> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
本例從事件處理器調(diào)用一個(gè)函數(shù):
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <head> <script> function changetext(id){ id.innerHTML="Ooops!"; } </script> </head> <body> <h1 onclick="changetext(this)">點(diǎn)擊文本!</h1> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
HTML 事件屬性
如需向 HTML 元素分配 事件,您可以使用事件屬性。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕執(zhí)行 <em>displayDate()</em> 函數(shù).</p> <button onclick="displayDate()">點(diǎn)這里</button> <script> function displayDate(){ document.getElementById("demo").innerHTML=Date(); } </script> <p id="demo"></p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
在上面的例子中,名為 displayDate 的函數(shù)將在按鈕被點(diǎn)擊時(shí)執(zhí)行。
使用 HTML DOM 來分配事件
HTML DOM 允許您使用 JavaScript 來向 HTML 元素分配事件:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <head> </head> <body> <p>點(diǎn)擊按鈕執(zhí)行 <em>displayDate()</em> 函數(shù).</p> <button id="myBtn">點(diǎn)這里</button> <script> document.getElementById("myBtn").onclick=function(){displayDate()}; function displayDate(){ document.getElementById("demo").innerHTML=Date(); } </script> <p id="demo"></p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
在上面的例子中,名為 displayDate 的函數(shù)被分配給 id=myButn" 的 HTML 元素。
按鈕點(diǎn)擊時(shí)Javascript函數(shù)將會(huì)被執(zhí)行。
onload 和 onunload 事件
onload 和 onunload 事件會(huì)在用戶進(jìn)入或離開頁面時(shí)被觸發(fā)。
onload 事件可用于檢測(cè)訪問者的瀏覽器類型和瀏覽器版本,并基于這些信息來加載網(wǎng)頁的正確版本。
onload 和 onunload 事件可用于處理 cookie。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body onload="checkCookies()"> <script> function checkCookies(){ if (navigator.cookieEnabled==true){ alert("Cookies 可用") } else{ alert("Cookies 不可用") } } </script> <p>彈窗-提示瀏覽器cookie是否可用。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
onchange 事件
onchange 事件常結(jié)合對(duì)輸入字段的驗(yàn)證來使用。
下面是一個(gè)如何使用 onchange 的例子。當(dāng)用戶改變輸入字段的內(nèi)容時(shí),會(huì)調(diào)用 upperCase() 函數(shù)。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <head> <script> function myFunction(){ var x=document.getElementById("fname"); x.value=x.value.toUpperCase(); } </script> </head> <body> 輸入你的名字: <input type="text" id="fname" onchange="myFunction()"> <p>當(dāng)你離開輸入框后,函數(shù)將被觸發(fā),將小寫字母轉(zhuǎn)為大寫字母。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
onmouseover 和 onmouseout 事件
onmouseover 和 onmouseout 事件可用于在用戶的鼠標(biāo)移至 HTML 元素上方或移出元素時(shí)觸發(fā)函數(shù)。
實(shí)例
一個(gè)簡(jiǎn)單的 onmouseover-onmouseout 實(shí)例:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:#D94A38;width:120px;height:20px;padding:40px;">Mouse Over Me</div> <script> function mOver(obj){ obj.innerHTML="Thank You" } function mOut(obj){ obj.innerHTML="Mouse Over Me" } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
onmousedown、onmouseup 以及 onclick 事件
onmousedown, onmouseup 以及 onclick 構(gòu)成了鼠標(biāo)點(diǎn)擊事件的所有部分。首先當(dāng)點(diǎn)擊鼠標(biāo)按鈕時(shí),會(huì)觸發(fā) onmousedown 事件,當(dāng)釋放鼠標(biāo)按鈕時(shí),會(huì)觸發(fā) onmouseup 事件,最后,當(dāng)完成鼠標(biāo)點(diǎn)擊時(shí),會(huì)觸發(fā) onclick 事件。
實(shí)例
一個(gè)簡(jiǎn)單的 onmousedown-onmouseup 實(shí)例:
更多實(shí)例
onmousedown 和onmouseup
當(dāng)用戶按下鼠標(biāo)按鈕時(shí),更換一幅圖像。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <head> <script> function lighton(){ document.getElementById('myimage').src="/upload/course/000/000/009/5804353cb2562758.gif"; } function lightoff(){ document.getElementById('myimage').src="/upload/course/000/000/009/580432b53cb5d221.gif"; } </script> </head> <body> <img id="myimage" onmousedown="lighton()" onmouseup="lightoff()" src="/upload/course/000/000/009/580432b53cb5d221.gif" width="100" height="180" /> <p>點(diǎn)擊不釋放鼠標(biāo)燈將一直亮著!</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
onload
當(dāng)頁面完成加載時(shí),顯示一個(gè)提示框。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <head> <script> function mymessage(){ alert("消息在 onload 事件觸發(fā)后彈出。"); } </script> </head> <body onload="mymessage()"></body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
onfocus
當(dāng)輸入字段獲得焦點(diǎn)時(shí),改變其背景色。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <head> <script> function myFunction(x){ x.style.background="yellow"; } </script> </head> <body> 輸入你的名字: <input type="text" onfocus="myFunction(this)"> <p>當(dāng)輸入框獲取焦點(diǎn)時(shí),修改背景色(background-color屬性) 將被觸發(fā)。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
鼠標(biāo)事件
當(dāng)指針移動(dòng)到元素上方時(shí),改變其顏色;當(dāng)指針移出文本后,會(huì)再次改變其顏色。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <h1 onmouseover="style.color='red'"onmouseout="style.color='black'">將鼠標(biāo)移至文字上</h1> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例