jQuery中的常用事件介紹
一、jQuery事件的分類
jQuery事件是對(duì)JavaScript事件的封裝,常用事件分類如下:
1、基礎(chǔ)事件
window事件。鼠標(biāo)事件。鍵盤事件。表單事件。
2、復(fù)合事件是多個(gè)事件的組合
鼠標(biāo)光標(biāo)懸停。鼠標(biāo)連續(xù)點(diǎn)擊。
二、鼠標(biāo)事件
鼠標(biāo)事件是當(dāng)用戶在文檔上面移動(dòng)或單擊鼠標(biāo)時(shí)而產(chǎn)生的事件,常用鼠標(biāo)事件有:
三、鍵盤事件
用戶每次按下或者釋放鍵盤上的按鍵時(shí)都會(huì)產(chǎn)生事件,常用鍵盤事件如下:
四、表單事件
當(dāng)元素獲得焦點(diǎn)時(shí),會(huì)觸發(fā)focus()事件,失去焦點(diǎn)時(shí),會(huì)觸發(fā)blur()事件。
表單提交時(shí)會(huì)觸發(fā)submit()事件。
五、綜合示例
需求說明:
- 1、用戶名輸入框獲得焦點(diǎn)時(shí)輸入框背景色為淺藍(lán)色,失去焦點(diǎn)時(shí)還原為白色背景色。
- 2、鼠標(biāo)移至登錄按鈕時(shí)字體變粗,移出時(shí)整體恢復(fù)正常。
- 3、敲擊鍵盤的“回車”鍵時(shí)觸發(fā)表單提交事件。
代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>事件演示示例</title> <style type="text/css"> #login{ width: 400px; height: 250px; background-color: #f2f2f2; border:1px solid #DDDDDD; padding: 5px; } #login fieldset { border: none; margin-top: 10px; } #login fieldset legend{ font-weight: bold; } #login fieldset p{ display: block; height: 30px; } #login fieldset p label { display: block; float:left; text-align: right; font-size: 12px; width: 90px; height: 30px; line-height: 30px; } #login fieldset p input { display: block; float:left; border: 1px solid #999; width: 250px; height: 30px; line-height: 30px; } #login fieldset p input.code{ width: 60px; } #login fieldset p img{ margin-left: 10px; } #login fieldset p a{ color: #057BD2; font-size: 12px; text-decoration: none; margin: 10px; } #login fieldset p input.btn{ background: url("./images/login.gif") no-repeat; width: 98px; height: 32px; margin-left: 60px; color: #ffffff; cursor: pointer; } #login fieldset p input.input_focus{ background-color: #BEE7FC; } </style> <!--引入jQuery--> <script src="../jquery-3.3.1.js"></script> <!--javascript--> <script> $(function(){ // 用戶名輸入框的焦點(diǎn)事件 $("input[name='member']").focus(function(){ $(this).addClass("input_focus"); }); // 用戶名失去焦點(diǎn) $("input[name='member']").blur(function(){ $(this).removeClass("input_focus"); }); // 鼠標(biāo)移入移出事件 $(".btn").mouseover(function(){ $(this).css("font-weight","bold"); }); $(".btn").mouseout(function(){ $(this).css("font-weight","normal"); }); // 鍵盤事件,敲擊回車鍵進(jìn)行表單提交,keyCode的數(shù)值代表不同的鍵盤按鍵 // js需要區(qū)分keyCode(IE)和which(FF)的兼容性,event.keyCode||event.which用來考慮兼容性 $(document).keypress(function(e){ if(e.keyCode==13){ //$("#login").submit(); // 模擬表單提交 alert("觸發(fā)表單的提交事件"); } }); }); </script> </head> <body> <form id="login"> <fieldset> <legend>用戶登錄</legend> <p> <label>用戶名:</label> <input name="member" type="text" /> </p> <p> <label>密碼:</label> <input name="password" type="text" /> </p> <p> <label>驗(yàn)證碼:</label> <input name="code" type="text" class="code" /> <img src="images/code.gif" width="80" height="30" /><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >換一張</a> </p> <p> <input name="" type="button" class="btn" value="登錄" /> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >注冊</a><span>|</span><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >忘記密碼?</a> </p> </fieldset> </form> </body> </html>
效果:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery中cookie用法實(shí)例詳解(獲取,存儲(chǔ),刪除等)
這篇文章主要介紹了jquery中cookie用法,結(jié)合實(shí)例詳細(xì)分析了jQuery操作cookie的獲取,存儲(chǔ),刪除等操作,并附帶了Jquery操作Cookie記錄用戶查詢過信息實(shí)現(xiàn)方法,需要的朋友可以參考下2016-01-01jquery 頁面滾動(dòng)到底部自動(dòng)加載插件集合
很多社交網(wǎng)站都使用無限滾動(dòng)的翻頁技術(shù)來提高用戶體驗(yàn),當(dāng)你頁面滑到列表底部時(shí)候無需點(diǎn)擊就自動(dòng)加載更多的內(nèi)容2014-01-01jQuery獲取某天的農(nóng)歷日期并判斷是否除夕或新年的方法
這篇文章主要介紹了jQuery獲取某天的農(nóng)歷日期并判斷是否除夕或新年的方法,涉及jQuery針對(duì)日期與時(shí)間的相關(guān)操作技巧,需要的朋友可以參考下2016-03-03jQuery實(shí)現(xiàn)CheckBox全選、全不選功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)CheckBox全選、全不選功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01$.each遍歷對(duì)象、數(shù)組的屬性值并進(jìn)行處理
通過$.each,可以遍歷對(duì)象、數(shù)組的屬性值并進(jìn)行處理,下面有個(gè)示例,需要的朋友可以參考下2014-07-07jQuery+json實(shí)現(xiàn)的簡易Ajax調(diào)用實(shí)例
這篇文章主要介紹了jQuery+json實(shí)現(xiàn)的簡易Ajax調(diào)用,結(jié)合實(shí)例形式分析了jQuery基于ajax實(shí)現(xiàn)json傳參調(diào)用的技巧,需要的朋友可以參考下2015-12-12