jquery的$().each和$.each的區(qū)別
$(selector).each(function(index,element))
這個函數(shù)和之前項目里面用到的遍歷數(shù)據(jù)的函數(shù)不是同一個呀(項目里面用到的函 數(shù):$.each(dataresource,function(index,element))),于是,就好好研究了下,果然在JS里面有兩個相似的函數(shù),于是也就有了今天的主題:
1.$(selector).each(function(index,element))
2.$.each(dataresource,function(index,element))
接下來就對這兩個函數(shù)做深入的探討:
1.$(selector).each(function(index,element))
作用:在dom處理上面用的較多
<ul id="each_id">
<li>muzi</li>
<li>digbig</li>
<li>muzidigbig</li>
</ul>
js遍歷函數(shù):
function traversalDOM(){
$("#each_id li").each(function(){
alert($(this).text())
});
}
traversalDOM();
輸出結(jié)果:

2.$.each(dataresource,function(index,element))
作用:在數(shù)據(jù)處理上用的比較多
示例:
此處沒有html代碼,只有js代碼,如下:
function traversalData(){
var jsonResourceList = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},{"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"}]';
if(jsonResourceList.length >0){
$.each(JSON.parse(jsonResourceList), function(index, currentObj) {
alert(currentObj.tagName);
});
}
}
traversalData()

3.最終結(jié)論:
在遍歷DOM時,通常用$(selector).each(function(index,element))函數(shù);
在遍歷數(shù)據(jù)時,通常用$.each(dataresource,function(index,element))函數(shù)。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- jQuery.parseJSON()函數(shù)詳解
- Jquery實現(xiàn)無縫向上循環(huán)滾動列表的特效
- jquery無縫圖片輪播組件封裝
- 在Vue項目中引入JQuery-ui插件的講解
- 在vue項目中使用Jquery-contextmenu插件的步驟講解
- Jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對象,Map()方法詳解以及數(shù)組中查詢某值是否存在
- Java中json與javaBean幾種互轉(zhuǎn)的講解
- Node.js Stream ondata觸發(fā)時機(jī)與順序的探索
- Node.js Event Loop各階段講解
- 使用jquery的cookie實現(xiàn)登錄頁記住用戶名和密碼的方法
相關(guān)文章
使用jQuery模板來展現(xiàn)json數(shù)據(jù)的代碼
通常我們在使用ajax的時候,都避免不了和json這種輕巧的數(shù)據(jù)格式打交道??墒峭謩拥娜ソ馕鰆son,構(gòu)建HTML,比較麻煩?,F(xiàn)在有了這個插件,就能像Extjs那樣使用模板解析json了。2010-10-10
JQuery對表格進(jìn)行操作的常用技巧總結(jié)
jQuery實現(xiàn)Flash效果上下翻動的中英文導(dǎo)航菜單代碼
基于jquery ui的alert,confirm方案(支持換膚)

