亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript HTML DOM 事件

事件

事件是用戶或?yàn)g覽器自身執(zhí)行的某種動(dòng)作,如click,load和mouseover都是事件的名字。

事件是javaScript和DOM之間的橋梁。

你若觸發(fā),我便執(zhí)行——事件發(fā)生,調(diào)用它的處理函數(shù)執(zhí)行相應(yīng)的JavaScript代碼給出響應(yīng)。

典型的例子有:頁面加載完畢觸發(fā)load事件;用戶單擊元素,觸發(fā)click事件。

對(duì)事件做出反應(yīng)

我們可以在事件發(fā)生時(shí)執(zhí)行 JavaScript,比如當(dāng)用戶在 HTML 元素上點(diǎn)擊時(shí)。

如需在用戶點(diǎn)擊某個(gè)元素時(shí)執(zhí)行代碼,請(qǐng)向一個(gè) HTML 事件屬性添加 JavaScript 代碼:

onclick=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í)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<head>
<script>
function changetext(id){
id.innerHTML="hello";
}
</script>
</head>
<body>
<h1 onclick="changetext(this)">點(diǎn)擊!</h1>
</body>
</html>

如需向 HTML 元素分配 事件,您可以使用事件屬性。

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>
<button onclick="displayDate()">點(diǎn)擊</button>
<script>
function displayDate(){
document.getElementById("demo").innerHTML=Date();
}
</script>
<p id="demo"></p>
</body>
</html>

HTML DOM 允許您使用 JavaScript 來向 HTML 元素分配事件:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<head>
</head>
<body>
<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>


onload 和 onunload 事件

onload 和 onunload 事件會(huì)在用戶進(jìn)入或離開頁面時(shí)被觸發(fā)。

onload 事件可用于檢測訪問者的瀏覽器類型和瀏覽器版本,并基于這些信息來加載網(wǎng)頁的正確版本。

onload 和 onunload 事件可用于處理 cookie。

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
<script type="text/javascript">
    function load(){
        alert("頁面加載完成");
      }
</script>
</head>
<body onload="load()">
</body>
</html>

onchange 事件

onchange 事件常結(jié)合對(duì)輸入字段的驗(yàn)證來使用。

<!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)你離開輸入框后,將小寫字母轉(zhuǎn)為大寫字母。</p>
</body>
</html>

onmouseover 和 onmouseout 事件

onmouseover 和 onmouseout 事件可用于在用戶的鼠標(biāo)移至 HTML 元素上方或移出元素時(shí)觸發(fā)函數(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:yellow;width:120px;height:20px;padding:40px;">Mouse Over Me</div>
<script>
function mOver(obj){
obj.innerHTML="Thank You"
}
function mOut(obj){
obj.innerHTML="鼠標(biāo)請(qǐng)移動(dòng)至此"
}
</script>
</body>
</html>

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 事件。

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
<script type="text/javascript">
   function noNumbers(e)
    {
    var keynum;
    var keychar;
    keynum = window.event ? e.keyCode : e.which;
    keychar = String.fromCharCode(keynum);
    alert(keynum+':'+keychar);
    }
</script>
</head>
<body>
<input type="text" onkeydown="return noNumbers(event)" />
</body>
</html>

其他事件:

onmousedown 和onmouseup
當(dāng)用戶按下鼠標(biāo)按鈕時(shí),更換一幅圖像。

onload
當(dāng)頁面完成加載時(shí),顯示一個(gè)提示框。

onfocus
當(dāng)輸入字段獲得焦點(diǎn)時(shí),改變其背景色。

鼠標(biāo)事件
當(dāng)指針移動(dòng)到元素上方時(shí),改變其顏色;當(dāng)指針移出文本后,會(huì)再次改變其顏色。


繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> function mouseOver() { document.getElementById('div1').style.border = "1px solid red"; } function mouseOut() { document.getElementById('div1').style.border = "1px solid white"; } </script> </head> <body> <div id="div1" style="width:300px;border:1px solid white;" onmouseover="mouseOver()" onmouseout="mouseOut()" > <p style="line-height:2em;text-align:center;">我是一些文字或者圖片</p> </div> </body> </html>
提交重置代碼