jQuery 事件
什么是事件?
頁(yè)面對(duì)不同訪問(wèn)者的響應(yīng)叫做事件。
事件處理程序指的是當(dāng) HTML 中發(fā)生某些事件時(shí)所調(diào)用的方法。
實(shí)例:
在元素上移動(dòng)鼠標(biāo)。
選取單選按鈕
點(diǎn)擊元素
在事件中經(jīng)常使用術(shù)語(yǔ)"觸發(fā)"(或"激發(fā)")例如: "當(dāng)您按下按鍵時(shí)觸發(fā) keypress 事件"。
常見(jiàn) DOM 事件:
鼠標(biāo)事件 ? ? ? ?click ? ? ? ??dblclick ??mouseenter ??mouseleave
鍵盤事件 ? ? ??keypress? ? ?keydown ???keyup ? ??? blur ??
表單事件 ? ? ? ?submit? ? ?? change ? ??? focus ? ??unload ? ?
文檔/窗口事件 ? ?? load ? ??? resize ? ???scroll ? ??
jQuery 事件方法語(yǔ)法
在 jQuery 中,大多數(shù) DOM 事件都有一個(gè)等效的 jQuery 方法。
頁(yè)面中指定一個(gè)點(diǎn)擊事件:
$("p").click();
下一步是定義什么時(shí)間觸發(fā)事件。您可以通過(guò)一個(gè)事件函數(shù)實(shí)現(xiàn):
$("p").click(function(){
? // 動(dòng)作觸發(fā)后執(zhí)行的代碼!!
});
常用的 jQuery 事件方法
$(document).ready()
$(document).ready() 方法允許我們?cè)谖臋n完全加載完后執(zhí)行函數(shù)。該事件方法在?jQuery 語(yǔ)法?章節(jié)中已經(jīng)提到過(guò)。
click()
click() 方法是當(dāng)按鈕點(diǎn)擊事件被觸發(fā)時(shí)會(huì)調(diào)用一個(gè)函數(shù)。
該函數(shù)在用戶點(diǎn)擊 HTML 元素時(shí)執(zhí)行。
在下面的實(shí)例中,當(dāng)點(diǎn)擊事件在某個(gè) <p> 元素上觸發(fā)時(shí),隱藏當(dāng)前的 <p> 元素:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").click(function(){ $(this).hide(); }); }); </script> </head>
dblclick()
當(dāng)雙擊元素時(shí),會(huì)發(fā)生 dblclick 事件。
dblclick() 方法觸發(fā) dblclick 事件,或規(guī)定當(dāng)發(fā)生 dblclick 事件時(shí)運(yùn)行的函數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").dblclick(function(){ $(this).hide(); }); }); </script> </head> <body> <p>雙擊鼠標(biāo),我就消失。</p> <p>雙擊我消失!</p> <p>雙擊我也消失!</p> </body> </html>
mouseenter()
當(dāng)鼠標(biāo)指針穿過(guò)元素時(shí),會(huì)發(fā)生 mouseenter 事件。
mouseenter() 方法觸發(fā) mouseenter 事件,或規(guī)定當(dāng)發(fā)生 mouseenter 事件時(shí)運(yùn)行的函數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").mouseenter(function(){ alert("您的鼠標(biāo)移到了 id=p1 的元素上!"); }); }); </script> </head> <body> <p id="p1">鼠標(biāo)指針進(jìn)入此處,會(huì)看到彈窗。</p> </body> </html>
?mouseleave()
當(dāng)鼠標(biāo)指針離開(kāi)元素時(shí),會(huì)發(fā)生 mouseleave 事件。
mouseleave() 方法觸發(fā) mouseleave 事件,或規(guī)定當(dāng)發(fā)生 mouseleave 事件時(shí)運(yùn)行的函數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").mouseleave(function(){ alert("再見(jiàn),您的鼠標(biāo)離開(kāi)了該段落。"); }); }); </script> </head> <body> <p id="p1">這是一個(gè)段落。</p> </body> </html>
mousedown()
當(dāng)鼠標(biāo)指針移動(dòng)到元素上方,并按下鼠標(biāo)按鍵時(shí),會(huì)發(fā)生 mousedown 事件。
mousedown() 方法觸發(fā) mousedown 事件,或規(guī)定當(dāng)發(fā)生 mousedown 事件時(shí)運(yùn)行的函數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").mousedown(function(){ alert("鼠標(biāo)在該段落上按下!"); }); }); </script> </head> <body> <p id="p1">這是一個(gè)段落</p> </body> </html>
mouseup()
當(dāng)在元素上松開(kāi)鼠標(biāo)按鈕時(shí),會(huì)發(fā)生 mouseup 事件。
mouseup() 方法觸發(fā) mouseup 事件,或規(guī)定當(dāng)發(fā)生 mouseup 事件時(shí)運(yùn)行的函數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").mouseup(function(){ alert("鼠標(biāo)在段落上松開(kāi)。"); }); }); </script> </head> <body> <p id="p1">這是一個(gè)段落。</p> </body> </html>
?hover()
hover()方法用于模擬光標(biāo)懸停事件。
當(dāng)鼠標(biāo)移動(dòng)到元素上時(shí),會(huì)觸發(fā)指定的第一個(gè)函數(shù)(mouseenter);當(dāng)鼠標(biāo)移出這個(gè)元素時(shí),會(huì)觸發(fā)指定的第二個(gè)函數(shù)(mouseleave)。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").hover(function(){ alert("你進(jìn)入了 p1!"); }, function(){ alert("拜拜! 現(xiàn)在你離開(kāi)了 p1!"); }); }); </script> </head> <body> <p id="p1">這是一個(gè)段落。</p> </body> </html>
?focus()
當(dāng)元素獲得焦點(diǎn)時(shí),發(fā)生 focus 事件。
當(dāng)通過(guò)鼠標(biāo)點(diǎn)擊選中元素或通過(guò) tab 鍵定位到元素時(shí),該元素就會(huì)獲得焦點(diǎn)。
focus() 方法觸發(fā) focus 事件,或規(guī)定當(dāng)發(fā)生 focus 事件時(shí)運(yùn)行的函數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").focus(function(){ $(this).css("background-color","#cccccc"); }); $("input").blur(function(){ $(this).css("background-color","#ffffff"); }); }); </script> </head> <body> Name: <input type="text" name="fullname"><br><br> Email: <input type="text" name="email"> </body> </html>
?blur()
當(dāng)元素失去焦點(diǎn)時(shí),發(fā)生 blur 事件。
blur() 方法觸發(fā) blur 事件,或規(guī)定當(dāng)發(fā)生 blur 事件時(shí)運(yùn)行的函數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").focus(function(){ $(this).css("background-color","#cccccc"); }); $("input").blur(function(){ $(this).css("background-color","#ffffff"); }); }); </script> </head> <body> Name: <input type="text" name="fullname"><br> Email: <input type="text" name="email"> </body> </html>