jQuery的事件處理你知道多少
一、jQuery的事件處理
1、頁面載入事件
$(document).ready() --- onload
2、事件綁定(bind)
bind(type,[data],fn)
type
:表示事件類型(click
、mouseover
、mouseout...
)
[data]
:可選參數(shù),表示傳遞給事件對象的額外數(shù)據(jù)
fn
:是一個函數(shù)(事件處理函數(shù)),當(dāng)事件發(fā)生時執(zhí)行的程序
為每一個匹配元素的特定事件(像click)綁定一個事件處理器函數(shù)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../jq/jquery.js"></script> </head> <body> <button id="btn">確定</button> <script> $(function(){ $('#btn').bind('click',function(){//可以給按鈕綁定其他事件 alert('事件綁定') }) }) </script> </body> </html>
顯示效果:點(diǎn)擊確定按鈕之后,出現(xiàn)彈窗
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../jq/jquery.js"></script> </head> <body> <img src="../img/1.jpg" alt="" width="150" height="200"> <script> $(function(){ //通過鼠標(biāo)的懸停、離開事件來改變img的圖像 $('img').bind('mouseover',function(){ $(this).attr({src:'../img/2.jpg'})//this表示的是img這個元素 }) $('img').bind('mouseout',function(){ $(this).attr({src:'../img/1.jpg'}) }) }) </script> </body> </html>
顯示效果:當(dāng)鼠標(biāo)懸停在圖片上時,顯示的是一個圖片。當(dāng)鼠標(biāo)離開這個圖片時,顯示的是另一張圖片。反復(fù)交替,沒有限制。
3、反綁定事件(unbind)
unbind([type],[data])
:刪除綁定的事件
(1)不帶參數(shù):刪除元素上綁定的所有事件
(2)帶參數(shù):[type]表示事件類型
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../jq/jquery.js"></script> </head> <body> <img src="../img/1.jpg" alt="" width="150" height="200"> <script> $(function(){ //通過鼠標(biāo)的懸停、離開事件來改變img的圖像 $('img').bind('mouseover',function(){ $(this).attr({src:'../img/2.jpg'})//this表示的是img這個元素 }) $('img').bind('mouseout',function(){ $(this).attr({src:'../img/1.jpg'}) }) $('img').unbind('mouseout')//解綁 }) </script> </body> </html>
顯示效果:鼠標(biāo)離開圖片之后,圖片不會變成1.jpg
4、一次性事件綁定(one)
綁定的事件只能執(zhí)行一次
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../jq/jquery.js"></script> </head> <body> <img src="../img/1.jpg" alt="" width="150" height="200"> <script> $(function(){ //通過鼠標(biāo)的懸停、離開事件來改變img的圖像 $('img').bind('mouseover',function(){ $(this).attr({src:'../img/2.jpg'})//this表示的是img這個元素 }) //一次性事件綁定 $('img').one('mouseout',function(){ $(this).attr({src:'../img/1.jpg'}) }) }) </script> </body> </html>
顯示效果:鼠標(biāo)離開圖片后,圖片會變成1.jpg,但是這種變化只會執(zhí)行一次。第二次離開圖片時,就不會變成1.jpg。
5、模擬鼠標(biāo)懸停(hover)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../jq/jquery.js"></script></head><body> <div style="width: 200px; height: 200px; background-color: red;"></div> <script> $(function(){ $('div').hover(function(){ $(this).css('backgroundColor','pink') }) }) </script></body></html><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../jq/jquery.js"></script> </head> <body> <div style="width: 200px; height: 200px; background-color: red;"></div> <script> $(function(){ $('div').hover(function(){ $(this).css('backgroundColor','pink') }) }) </script> </body> </html>
顯示效果:鼠標(biāo)懸停在圖片上時,圖片由紅色變?yōu)榉凵?。離開圖片時并不會變回原來的紅色。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
jQuery實(shí)現(xiàn)簡易的天天愛消除小游戲
貌似最近騰訊手機(jī)游戲天天愛消除挺火的,我也是粉絲之一,最近對javascript一直比較感興趣然后想用js仿造一個,應(yīng)該不是太難,2015-10-10jquery實(shí)現(xiàn)倒計(jì)時代碼分享
最近做的項(xiàng)目,需要倒計(jì)時,翻了翻資料,寫了出來,分享給大家,歡迎拍磚,jquery庫自己記得引用哈2014-06-06jQuery調(diào)用RESTful WCF示例代碼(GET方法/POST方法)
本篇文章主要介紹了jQuery調(diào)用RESTful WCF示例代碼(GET方法/POST方法),需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01