$(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. Ubuntu 15.10 下Scala 操作Redis Cluster

    1 前言 Redis Standalone,Redis Cluster的安装在前面介绍过,地址:http://www.cnblogs.com/liuchangchun/p/5063477.html,这 ...

  2. kong api可视化管理工具konga安装

    说明:官网推荐: kong-dashboard,但对比界面高端程度和友好度,更推荐konga.[一个坑]kong版本问题:我在安装时目前kong最新版本已经到1.0.0, 对于konga和kong-d ...

  3. Halcon 17与 c# 混合编程

    这篇主要是C#和Halcon的混合编程,在此基础上对按键不同功能的划分,以及图片适应窗口和从本地打开图片. halcon源程序:   dev_open_window(0, 0, 512, 512, ' ...

  4. Java中的升序和降序

    package ah; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; impo ...

  5. 在chrome console中添加jQuery

    由于现有seajs等封装,jQuery等已不在全局暴露,即使网站中已使用jQuery,在console也使用不了. 在chrome中可以用以下代码加入jQuery: fetch('http://cod ...

  6. python-pytest学习

    一:pytest基于unittest之上的单元测试框架1.自动发现测试模块和测试方法:2.断言使用assert+表达式即可:3.可以设置会话级.模块级.类级.函数级的fixtures :数据准备+清理 ...

  7. scrapy爬去京东书籍信息

    # -*- coding: utf-8 -*- import scrapy import urllib import json from copy import deepcopy class JdSp ...

  8. OSS RFC

    This is a very late reply, but just want to share the process of setting up all associated RFCs with ...

  9. 《python编程快速上手》

    第一部分 编程基础 @表达式 ** % // @ >>> int(3.4) 3 >>>round(3.555,2)3.56 @判断条件时:0和0.0和‘’都是Fal ...

  10. python-day2列表、元祖、字典;编码;字符串

    @导入模块时,会先搜索目前路径的同名py文件,再去全局环境变量找 @看模块的环境变量 import sys print(sys.path) @site-package存放第三方库,可以把自己建的拷贝在 ...