博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
$.each 和$(selector).each()的差别
阅读量:7051 次
发布时间:2019-06-28

本文共 3334 字,大约阅读时间需要 11 分钟。

» »
$.each()

$.each()

  • Posted on 2012 年 3 月 15 日 in ,
  • |
  • by
  • |

译自官方手冊:

对数组或对对象内容进行循环处理

jQuery.each( collection, callback(indexInArray, valueOfElement) )

collection   遍历的对象或数组

callback(indexInArray, valueOfElement) 在每个对象上调用的函数

说明

一个通用的遍历函数 , 能够用来遍历对象和数组. 数组和含有一个length属性的伪数组对象 (伪数组对象如function的arguments对象)以数字索引进行遍历,从0到length-1, 其他的对象通过的属性进行遍历.

$.each()与不同, 后者专用于jquery对象的遍历, 前者可用于遍历不论什么的集合(不管是数组或对象),假设是数组,回调函数每次传入数组的索引和相应的值(值亦能够通过this keyword获取,但javascript总会包装this 值作为一个对象—虽然是一个字符串或是一个数字),方法会返回被遍历对象的第一參数

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//样例:———传入数组

<!DOCTYPE html>

<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>

$.each([52, 97], function(index, value) {

alert(index + ‘: ‘ + value);
});

</script>

</body>
</html>

//输出

0: 52

1: 97

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//样例:———假设一个映射作为集合使用,回调函数每次传入一个键-值对

<!DOCTYPE html>

<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>

var map = {

‘flammable’: ‘inflammable’,
‘duh’: ‘no duh’
};
$.each(map, function(key, value) {
alert(key + ‘: ‘ + value);
});

</script>

</body>
</html>

//输出

flammable: inflammable

duh: no duh

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//样例:———回调函数中 return false时能够退出$.each(), 假设返回一个非false 即会像在for循环中使用continue 一样, 会马上进入下一个遍历

<!DOCTYPE html>

<html>

<head>

  <style>

  div { color:blue; }

  div#five { color:red; }

  </style>

  <script src=”http://code.jquery.com/jquery-latest.js”></script>

</head>

<body>

  <div id=”one”></div>

  <div id=”two”></div>

  <div id=”three”></div>

  <div id=”four”></div>

  <div id=”five”></div>

<script>

    var arr = [ "one", "two", "three", "four", "five" ];//数组

    var obj = { one:1, two:2, three:3, four:4, five:5 }; // 对象

    jQuery.each(arr, function() {  // this 指定值

      $(“#” + this).text(“Mine is ” + this + “.”);  // this指向为数组的值, 如one, two

       return (this != “three”); // 假设this = three 则退出遍历

   });

    jQuery.each(obj, function(i, val) {  // i 指向键, val指定值

      $(“#” + i).append(document.createTextNode(” – ” + val));

    });

</script>

</body>

</html>

// 输出

Mine is one. – 1
Mine is two. – 2
Mine is three. – 3
- 4
- 5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//样例:———遍历数组的项, 传入index和value

<!DOCTYPE html>
<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>

$.each( ['a','b','c'], function(i, l){

alert( “Index #” + i + “: ” + l );
});

</script>

</body>
</html>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//样例:———遍历对象的属性,传入 key和value

<!DOCTYPE html>

<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>

$.each( { name: “John”, lang: “JS” }, function(k, v){

alert( “Key: ” + k + “, Value: ” + v );
});

</script>

</body>
</html>

正自评论的样例:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1. 假设不想输出第一项 (使用retrun true)进入 下一遍历

<!DOCTYPE html>

<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>

var myArray=["skipThis", "dothis", "andThis"];

$.each(myArray, function(index, value) {
if (index == 0) {
return true; // equivalent to ‘continue’ with a normal for loop
}
// else do stuff…
alert (index + “: “+ value);
});

</script>

</body>
</html>

转载地址:http://resol.baihongyu.com/

你可能感兴趣的文章
Outlook Anywhere 客户端配置详解
查看>>
IOS在Xcode 4.x以上如何 创建 和 添加 静态库
查看>>
WebSphere was 7.0修改端口号为80,修改上下文根
查看>>
Repeater控件数据导出Excel
查看>>
下载Android源码出现的问题
查看>>
远程桌面如何复制本地文件 远程桌面拷贝电脑上的文件方法
查看>>
[转]解决JS浮点数(小数)计算加减乘除的BUG
查看>>
ASP.NET MVC应用程序的安全性介绍总括(高级编程)
查看>>
Java模拟Delegate
查看>>
记录下,我们平时开发当中不得不知道的HTTP状态码
查看>>
HDU-1045 Fire NetFire Net 最大团
查看>>
VC++6.0 按F1无法打开 MSDN 的解决办法
查看>>
Eclipse自动部署项目到Tomcat的webapps下的有效方法
查看>>
Extjs4快速上手三——实现主界面
查看>>
如何替代Flex?
查看>>
控制属性修改时间,控制时间,联合主键
查看>>
android-包签名
查看>>
Android开发和调试必备工具-SDK Tools
查看>>
【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
查看>>
《微软的软件测试之道》读书笔记 之 结构测试技术
查看>>