標籤:
常用事件:
● onclick 元素點擊時
● onfocus 元素獲得焦點時
● onblur 元素失去焦點時
● onmouseover 滑鼠經過時
● onsubmit 表單提交時(<form onsubmit="return func();"></form>)
● onload 頁面載入完畢時
註:onsubmit = "return func()"; func() 函數才能阻攔提交的結果。
【例】onload 的缺點:
img.php,用於展示圖片,這裡讓圖片 5 秒之後輸出
<?php$img = imagecreatetruecolor(300, 300); //建立一個300 * 300 px 的黑色方塊sleep(5); //5 秒之後輸出header("content-type:image/png");imagepng($img);
test.html,引用圖片,同時使用 onload 事件改變文字效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script> function t(){ document.getElementById("p").style.fontWeight = "bold"; } </script></head><body onload="t();"> <img src="img.php" > <p id="p">定時改變文字</p> </body></html>
此時訪問 test.html,頁面顯示:
5 秒之後:
結論:onload 事件必須要等頁面內所關聯的 比如 src 引用來的不論是本站還是外站的全部的元素都載入完畢之後,才執行。
附:sublime (text2 | test3)快速鍵:
① 輸入:
p>input:text
按 tab ,得到:
<p><input type="text" name="" id=""></p>
② 輸入:
p*2>input:text
按 tab,得到:
<p><input type="text" name="" id=""></p><p><input type="text" name="" id=""></p>
Javascript 筆記與總結(2-14)事件