$(selector).each(function(index,element))

定义和用法

each() 方法规定为每个匹配元素规定运行的函数。

$(selector).each(function(index,element))

参数 描述
function(index,element)

必需。为每个匹配元素规定运行的函数。

  • index - 选择器的 index 位置
  • element - 当前的元素(也可使用 "this" 选择器)

作用:在dom方面用的较多

示例

html部分文档

<ul id="each_id">
<li>Coffee</li>
<li>Soda</li>
<li>Milk</li>
</ul>

js遍历函数:

$("li").each(function(){
      alert($(this).text())
    });

效果

$.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, obj) {
    alert(obj.tagName);
});
}
}

输出结果:

最终结论:

在遍历DOM时,通常用$(selector).each(function(index,element))函数;

在遍历数据时,通常用$.each(dataresource,function(index,element))函数。

参考自:http://www.w3school.com.cn/jquery/traversing_each.asp

https://blog.csdn.net/gao_xu_520/article/details/78588977

jquery的$(selector).each(function(index,element))和$.each(dataresource,function(index,element))的区别的更多相关文章

  1. jQuery Custom Selector JQuery自定义选择器

    什么是JQuery的选择器呢,详见JQuery中的Selector: http://docs.jquery.com/Selectors 比如 $("div:contains('John')& ...

  2. jquery 选择器(selector)和事件(events)

    页面加载完成后开始运行do stuff when DOM is ready 中的语句! $(document).ready(function() {       // do stuff when DO ...

  3. jQuery.Deferred exception: $.get is not a function TypeError: $.get is not a function

    /********************************************************************** * jQuery.Deferred exception: ...

  4. DOM方法index()相关问题(为何$(this).index(this)是错误的 )

    写jQuery的时候遇到一个关于index()的问题,查找相关资料后,解决了,把自己的想法写在下面. index() 方法返回指定元素相对于其他指定元素的 index 位置. 完全语法为:$(sele ...

  5. 2、jQuery的基本概念-必看-版本-入口函数- jq对象和dom对象区别

    1.4. jQuery的版本 官网下载地址:http://jquery.com/download/ jQuery版本有很多,分为1.x 2.x 3.x 大版本分类: 1.x版本:能够兼容IE678浏览 ...

  6. ORA-01502: index 'INDEX_NAME' or partition of such index is in unusable state

    ORA-01502: index 'INDEX_NAME' or partition of such index is in unusable state 原因: 这个错误一般是因为索引状态为UNUS ...

  7. ORA-01502: index ‘index_name' or partition of such index is in unusable state

    错误现象: 今天发布脚本时,一个表插入数据时报如下错误 ORA-01502: index ‘index_name' or partition of such index is in unusable ...

  8. Function接口 – Java8中java.util.function包下的函数式接口

    Introduction to Functional Interfaces – A concept recreated in Java 8 Any java developer around the ...

  9. JS function的定义方法,及function对象的理解。

    废话篇: 今天看到了Function的内容,各种晕,各种混淆有木有.简直是挑战个人脑经急转弯的极限啊.不过,最终这一难题还是被我攻克了,哇咔咔.现在就把这东西记下来,免得到时候又忘了就悲催了.... ...

随机推荐

  1. Butterknife--Android Butterknife使用方法总结(转)

    原文链接:http://blog.csdn.net/donkor_/article/details/77879630 前言: ButterKnife是一个专注于Android系统的View注入框架,以 ...

  2. 算法——001BitMap(位图)算法

    哈希表在查找定位操作上具有O(1)的常量时间,常用于做性能优化,但是内存毕竟是有限的,当数据量太大时用哈希表就会内存溢出了.而考虑对这些大数据进行存盘分批处理又有IO上的开销,性能又不能满足要求.这个 ...

  3. c++字节对齐

    参考URL: http://blog.csdn.net/hairetz/article/details/4084088 0 字节对齐的意义按我的理解是便于cpu一次取完所有数据, 提高代码的执行效 1 ...

  4. 数学模块_math

    ceil 进一, 向上取整 floor 向下取整 pow(x, y) x的y次方 print(math.pow(2, 3)) # 8.0 sqrt(x) x的开平方(结果为浮点数) print(mat ...

  5. 优化读取纯真IP数据库QQWry.dat获取地区信息

    改自HeDaode 2007-12-28的代码 将之改为从硬盘读取后文件后,将MemoryStream放到内存中,提高后续查询速度 ///<summary> /// 提供从纯真IP数据库搜 ...

  6. Sublime Text 3安装Package Control

    Package Control官网:https://packagecontrol.io/installation#st3 ctrl + ` 或者 View > Show Console呼出控制台 ...

  7. WebApp的自动测试工具: Jenkins

    一.下载并安装(msi)https://jenkins.io/download/thank-you-downloading-windows-installer-stable/ 在安装过程这, 需要从p ...

  8. 以超级管理员方式运行bat文件

    以超级管理员方式直接运行bat 脚本文件,直接上脚本代码,以下是以一个服务的安装为例子 @echo =============================== ¶¯Ì¬ÅŲú·þÎñ ===== ...

  9. CentOS7 部署zabbix4.2

    zabbix我就不介绍了吧,但是可能又有些小白,我还是介绍一下吧,嘿嘿! 一:什么是zabbix及优缺点(对比cacti和nagios) Zabbix能监视各种网络参数,保证服务器系统的安全运营:并提 ...

  10. java.lang.OutOfMemoryError: PermGen space解决方法