$(selector).each(function(index,element))
这个函数和之前项目里面用到的遍历数据的函数不是同一个呀(项目里面用到的函 数:$.each(dataresource,function(index,element))),于是,就好好研究了下,果然在js里面有两个相似的函数,于是也就有了今天的主题:
1.$(selector).each(function(index,element))
2.$.each(dataresource,function(index,element))
接下来就对这两个函数做深入的探讨:
1.$(selector).each(function(index,element))
作用:在dom处理上面用的较多
<ul id="each_id"> <li>muzi</li> <li>digbig</li> <li>muzidigbig</li> </ul>
js遍历函数:
function traversaldom(){ $("#each_id li").each(function(){ alert($(this).text()) }); } traversaldom();
输出结果:
2.$.each(dataresource,function(index,element))
作用:在数据处理上用的比较多
示例:
此处没有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.最终结论:
在遍历dom时,通常用$(selector).each(function(index,element))函数;
在遍历数据时,通常用$.each(dataresource,function(index,element))函数。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接
如对本文有疑问, 点击进行留言回复!!
JavaScript 好题汇总分享(持续更新,看到好题就写)
XMLHttpRequest 2级 &&进度事件&&JSONP
使用递归原生实现拷贝&&最简单的方法实现深拷贝
网友评论