常用的JavaScript事件例子,自己理解,如果有不對或不足希望大家指出來!
onmouseover、onmouseout、onchange、onfocus、onblur、onsubmit、onclick、onload和onunload
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>javascript常用驗證</title>
- <style>
- </style>
- <script type="text/javascript">
- function mouseOver(){
- alert("當鼠標移入時觸發事件...");
- }
- function mouseOut(){
- alert("當鼠標移出時出發事件...");
- }
- function checkEmail(str){
- if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
- return alert("you are right!!");
- else
- alert("oh,no!!!");
- }
- function
- alert("哈哈,我是onload事件,在頁面加載時會執行我的....")
- }
- function onUnloadEvent(){
- alert("哈哈,我是unonload事件,在頁面離開的時候會執行的....");
- }
- function onClickEvent(){
- alert("我是鼠標點擊事件哈哈...");
- }
- function a(){
- document.getElementById("trId").style.cssText="background-Color:#00F";
- }
- function b(){
- document.getElementById("trId").style.cssText="background-Color:#fff";
- }
- function c(){
- document.getElementById("inputId").focus();
- document.getElementById("inputId").select();
- }
- function setFocusStyle(){
- document.getElementById("lname").style.background="yellow";
- }
- function setFocusStyle1(){
- document.getElementById("fname").style.background="yellow";
- }
- function setOnblurStyle(){
- document.getElementById("lname").style.background="red";
- }
- function setOnblurStyle1(){
- document.getElementById("fname").style.background="red";
- }
- </script>
- </head>
- <body onload="onloadEvent()">
- <a href="http://www.w3school.com.cn" onmouseover="mouseOver()">你點我啊,當鼠標被移動到某個元素自上時觸發的事件</a><br><br>
- <a href="http://www.w3school.com.cn" onmouseout="mouseOut()">你點我啊,當鼠標從某個元素上面一開時觸發的事件</a><br><br>
- <a href="http://www.w3school.com.cn" onmouseout="mouseOver()">你點我啊</a><br><br>
- onchange事件(當用戶改變某個域的內容時觸發的事件):<br>
- <input type="text" id="email" size="30" onchange="checkEmail(this.value)" value=""/><br><br>
- <form>
- <table border="1" bgcolor="" width="30%" class="">
- <tr><td align="center">a1</td></tr>
- <tr><td align="right">a2</td></tr>
- <tr><td align="right">a3</td></tr>
- <tr><td align="left">a4</td></tr>
- <tr><input type="button" id="buttonId" value="OK5" onClick="onClickEvent()"/></tr>
- </table>
- </form>
- <table class="table2" cellpadding="0" border="1" bordercolor="#0033FF" >
- <tr align="center" onmouseover="this.style.backgroundColor='#00F';" onmouseout="this.style.backgroundColor='#fff';">
- <th class="th1" align="center">登記</th>
- <th class="th1" align="center">通過鼠標移入移出事件來改變行的顏色</th>
- </tr>
- <tr onmouseover="this.style.backgroundColor='#00F';" onmouseout="this.style.backgroundColor='#fff';">
- <th>A0000001</th>
- <td>鼠標移入我變色</td>
- </tr>
- <tr id="trId" onmouseover="a()" onmouseout="b()">
- <th>A0000002</th>
- <td>鼠標移出我又變色</td>
- </tr>
- </table>
- <br>
- 自身獲得焦點,但獲得焦點的標誌是該文本框內出現輸入光標<br>
- 如果要讓裏面的內容全部被選中,還必須使用this.select();<br>
- <input type="text" name="url" value="http://www.gxblk.com" size="30" onmousemove="this.focus();this.select();"><br>
- <input id="inputId" type="text" name="url" value="http://www.gxblk.com" size="30" onmousemove="this.focus();this.select();"><br>
- onfocus事件舉例(當點擊輸入框的時候獲得焦點會觸發獲得焦點事件,當點擊到別處時會失去焦點會觸發失去焦點事件):<br>
- First Name:<input id="fname" type="text" onfocus="setFocusStyle1()" onblur="setOnblurStyle1()"><br>
- Last Name:<input id="lname" type="text" onfocus="setFocusStyle()" onblur="setOnblurStyle()"/><br>
- onsubmit與onclick的區別:onsubmit是表單提交時觸發,onclick是按鈕點擊時觸發(onclick對button有效):<br>
- 一個是提交表單操作,把東西傳給服務器,一個是鼠標的單擊操作;<br>
- 當然,onclick可以通過js來觸發onsubmit事件,如通過onclick觸發一個函數,在函數裏面調用document.form.submit();就可以觸發表單提交事件呵..<br>
- onsubmit事件用法:<input type="submit" value="submit" />
- <form name="form" action="#" method="post" onsubmit="check();">
- <p><label>稱呼: </label><input type="text" name="name" id="name" /></p>
- <p><label>發表留言:</label><textarea name="contents" id="contents" cols="15" rows="5"></textarea><</p>
- <p class="noborder"><input type="submit" value="submit" /><input type="reset" value="reset"/><p>
- </form>
- <script type="text/javascript">
- function check(){
- var name=document.getElementById("name").value;
- var contens=document.getElementById("contents").value;
- if (name=="")
- {
- alert("用戶名不能爲空onsubmit");
- exit();
- }
- if (content=="")
- {
- alert("內容不能爲空onsubmit");
- exit();
- }
- }
- </script>
- onclick事件用法:<input type="button" value="submit" onclick="check1()"/>
- <form name="form" action="#" method="post">
- <p><label>稱呼:</label><input type="text" name="name" id="name" /></p>
- <p><label>發表留言:</label><textarea name="contents" id="contents" cols="15" rows="5"></textarea></p>
- <p>
- <input type="button" value="submit" onclick="check1()"/>
- <input type="reset" value="reset"/><p>
- </form>
- <script type="text/javascript">
- function check1(){
- var name=document.getElementById("name").value;
- var contents=document.getElementById("contents").value;
- if (name=="")
- {
- alert("用戶名不能爲空
- }
- if (contents=="")
- {
- alert("內容不能爲空
- }
- }
- </script>
- </body>
- </html>