1. onfocus 事件在對象獲得焦點時發生。
實例:
<html> <head> <script type="text/javascript"> function setStyle(x) { document.getElementById(x).style.background="yellow" } </script> </head> <body> First name: <input type="text" onfocus="setStyle(this.id)" id="fname" /><br /> Last name: <input type="text" onfocus="setStyle(this.id)" id="lname" /> </body> </html>
結果:當用鼠標點擊input窗口的時候 input背景顏色為黃色
2. onblur 事件會在對象失去焦點時發生;
實例:
<html> <head> <script type="text/javascript"> function upperCase() { var x=document.getElementById("fname").value document.getElementById("fname").value=x.toUpperCase() //toUpperCase() 方法用於把字符串轉換為大寫
}
</script>
</head>
<body>
輸入您的姓名: <input type="text" id="fname" onblur="upperCase()"/>
</body>
</html>
結果:在input窗口中輸入小寫的英文 當input失去焦點的時候 將剛輸入的input的value內容轉成大寫
3.onload 事件會在頁面或圖像加載完成后立即發生。
window.onload 回調函數其實是在頁面加載完成后(包括圖片內容的顯示)才會執行,並不是頁面加載的等待過程中就執行。
例:
<html>
<head>
<script type="text/javascript">
function load()
{
window.status="Page is loaded"
}
</script>
</head>
<body onload="load()"
>
</body>
</html>
onload,加載圖片,加載整個body中的內容,所以加在body上