深入理解jquery中的each用法
1種 通過(guò)each遍歷li 可以獲得所有l(wèi)i的內(nèi)容
<!-- 1種 --> <ul class="one"> <li>11a</li> <li>22b</li> <li>33c</li> <li>44d</li> <li>55e</li> </ul> <button>輸出每個(gè)li值</button> <script> // 1種 通過(guò)each遍歷li 可以獲得所有l(wèi)i的內(nèi)容 $("button").click(function(){ $(".one > li").each(function(){ // 打印出所有l(wèi)i的內(nèi)容 console.log($(this).text()); }) }); </script>
2種 通過(guò)each遍歷li 通過(guò)$(this)給每個(gè)li加事件
<!-- 2種 --> <ul class="two"> <li>2222</li> <li>22b</li> <li>3333</li> <li>44d</li> <li>5555</li> </ul> <script> // 2種 通過(guò)each遍歷li 通過(guò)$(this)給每個(gè)li加事件 $('.two > li').each(function(index) { console.log(index +":" + $(this).text()); // 給每個(gè)li加click 點(diǎn)那個(gè)就變顏色 $(this).click(function(){ alert($(this).text()); $(this).css("background","#fe4365"); }); }); </script>
4種 遍歷所有l(wèi)i 給所有l(wèi)i添加 class類(lèi)名
<!-- 4種 --> <ul class="ctn3"> <li>Eat</li> <li>Sleep</li> <li>3種</li> </ul> <span>點(diǎn)擊3</span> <script> // 4種 遍歷所有l(wèi)i 給所有l(wèi)i添加 class類(lèi)名 $('span').click(function(){ $('.ctn3 > li').each(function(){ $(this).toggleClass('example'); }) }); </script>
5種 在each()循環(huán)里 element == $(this)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>each練習(xí)2</title> <style> div { width: 40px; height: 40px; margin: 5px; float: left; border: 2px blue solid; text-align: center; } span { width: 40px; height: 40px; color: red; } </style> </head> <body> <div></div> <div></div> <div></div> <div id="stop">Stop here</div> <div></div> <div></div> <button>Change colors</button> <span></span> </body> <script src="jquery-1.11.1.min.js"></script> <script > // 在each()循環(huán)里 element == $(this) $('button').click(function(){ $('div').each(function(index,element){ //element == this; $(element).css("background","yellow"); if( $(this).is("#stop")){ $('span').text("index :" + index); return false; } }) }) </script> </html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- JavaScript使用forEach()與jQuery使用each遍歷數(shù)組時(shí)return false 的區(qū)別
- jQuery的Each比JS原生for循環(huán)性能慢很多的原因
- jQuery通用的全局遍歷方法$.each()用法實(shí)例
- 淺談jquery中的each方法$.each、this.each、$.fn.each
- jQuery篩選數(shù)組之grep、each、inArray、map的用法及遍歷json對(duì)象
- jQuery使用each方法與for語(yǔ)句遍歷數(shù)組示例
- jquery 遍歷數(shù)組 each 方法詳解
- jQuery中each()、find()和filter()等節(jié)點(diǎn)操作方法詳解(推薦)
- jQuery使用$.each遍歷json數(shù)組的簡(jiǎn)單實(shí)現(xiàn)方法
- jQuery.each使用詳解
- jquery使用each方法遍歷json格式數(shù)據(jù)實(shí)例
相關(guān)文章
基于JQuery的日期聯(lián)動(dòng)實(shí)現(xiàn)代碼
基于JQuery的日期聯(lián)動(dòng)實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-02-02jQuery選擇器中含有空格的使用示例及注意事項(xiàng)
選擇器中的空格是不容忽視的,多一個(gè)空格或少一個(gè)空格也許得到的結(jié)果會(huì)截然不同的,下面以一個(gè)示例為大家詳細(xì)介紹下到底有什么不同,感興趣的朋友額可以參考下2013-08-08jquery入門(mén)—選擇器實(shí)現(xiàn)隔行變色實(shí)例代碼
JQuery入門(mén)—選擇器實(shí)現(xiàn)隔行變色如何實(shí)現(xiàn)呢?JQuery選擇器繼承了CSS、path語(yǔ)音的部分語(yǔ)法,允許通過(guò)標(biāo)簽名、屬性名、內(nèi)容對(duì)DOM元素進(jìn)行快速、準(zhǔn)確的選擇,接下來(lái)詳細(xì)介紹,需要的朋友可以參考下2013-01-01jQuery實(shí)現(xiàn)別踩白塊兒網(wǎng)頁(yè)版小游戲
本文主要介紹了jQuery實(shí)現(xiàn)別踩白塊兒網(wǎng)頁(yè)版小游戲的思路分析與代碼。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01淺析jQuery Ajax請(qǐng)求參數(shù)和返回?cái)?shù)據(jù)的處理
這篇文章主要介紹了淺析jQuery Ajax請(qǐng)求參數(shù)和返回?cái)?shù)據(jù)的處理的相關(guān)資料,需要的朋友可以參考下2016-02-02