本文摘自PHP中文网,作者青灯夜游,侵删。
区别:在遍历DOM时,通常用$(selector).each()函数,该函数在dom处理上面用的较多;在遍历数据时,通常用$.each()函数,该函数在数据处理上用的比较多。

【相关推荐:jQuery视频教程】
在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法。
$().each 在dom处理上面用的较多。如果页面有多个input标签类型为checkbox,对于这时用$().each来处理多个checkbook,例如:
1 2 3 4 5 6 | {
if ($(this).attr(‘checked’)==true)
{
}
回调函数是可以传递参数,i就为遍历的索引。
|
遍历一个数组通常用$.each()来处理,例如:
1 2 3 4 | $.each([{name: "limeng" ,email: "xfjylimeng" },{name: "hehe" ,email: "xfjylimeng" }], function (i,n)
{
alert( "索引:" +i+ "对应值为:" +n.name);
});
|
参数i为遍历索引值,n为当前的遍历对象.
1 2 3 4 | var arr1 = [ "one" , "two" , "three" , "four" , "five" ];
$.each(arr1, function (){
alert( this );
});
|
输出:one two three four five
1 2 3 4 | var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$.each(arr2, function (i, item){
alert(item[0]);
});
|
输出:1 4 7
1 2 3 4 | var obj = { one:1, two:2, three:3, four:4, five:5 };
$.each(obj, function (key, val) {
alert(obj[key]);
});
|
输出:1 2 3 4 5
更多编程相关知识,请访问:编程视频!!
以上就是jquery的$().each和$.each的区别是什么?的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
jQuery如何判断checkbox是否选中
jQuery中liger ui是什么
jQuery如何判断对象是否存在问题
jQuery怎样才能快速滚动到页面顶部
如何用jQuery选择节点下的第几个子元素
jQuery怎么判断值是否在数组中
如何解决jQuery不兼容问题
jQuery如何让按钮不可用
jQuery load中文乱码怎么办
使用jQuery实现网站导航抖动效果
更多相关阅读请进入《jQuery》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » jquery的$().each和$.each的区别是什么?