jquery的$().each和$.each的区别
程序员文章站
2023-11-23 23:18:22
$(selector).each(function(index,element))
这个函数和之前项目里面用到的遍历数据的函数不是同一个呀(项目里面用到的函 ...
$(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))函数。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
上一篇: Android 定时器实现图片的变换
推荐阅读
-
jsp和servlet的区别探讨
-
call和apply的区别是什么(apply的用法和搭配)
-
jsp和servlet区别在哪(简述2者间的不同处)
-
h5和html的区别有哪些(2者特点分析及区别介绍)
-
ios原生开发和前端开发(app原生开发和非原生的区别)
-
C#中new和override的区别个人总结
-
C#中sleep和wait的区别分析
-
ai文件怎么保存成jpg? AI保存jpg格式和PS的区别
-
FOXMAIL收邮件报错:The capacity of each mail box should not exceed 2G的解决方法
-
PHP函数addslashes和mysql_real_escape_string的区别