jQuery的遍历方法
1、jQuery中的map使用方法
<!DOCTYPE html>
<html>
<head>
<style>p { color:red; }</style>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head> <body>
<form method="post" action="">
<fieldset>
<div>
<label for="two"></label>
<input type="checkbox" value="" id="two" name="number[]">
</div>
<div>
<label for="four"></label>
<input type="checkbox" value="" id="four" name="number[]">
</div>
<div>
<label for="six"></label>
<input type="checkbox" value="" id="six" name="number[]">
</div>
<div>
<label for="eight"></label>
<input type="checkbox" value="" id="eight" name="number[]">
</div>
</fieldset>
</form> <p><b>Values: </b></p> <script>
$("p").append($(':checkbox').map(function() {
return this.id;
}).get().join(','));
</script> </body>
</html>
jQuery通过对当前集合中的每个元素调用函数对象,写法
.map(callback(index,element))
2、forEach方法
这个方法只能在非IE浏览器中使用,IE浏览器中需要写个支持,应用于数组的遍历,调用数组中的每个元素
array.forEach(callback[, thisObject]);
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError(); var thisp = arguments[];
for (var i = ; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
function printBr(element, index, array) {//数组元素,元素的索引,数组本身
document.write("<br />[" + index + "] is " + element );
} [, , , , ].forEach(printBr);
//这里是示例代码
//这里写了一个支持方法
注意:forEach无法在所有元素都传递给调用的函数之前终止(而for循环却有break方法),如果要提前终止,必须把forEach放在try块中,并能抛出一个异常。如果forEach()调用的函数抛出foreach.break异常,循环会提前终止:(也可以考虑用if+return的方式来跳出)
function foreach(a,b,c){
try{
a.forEach(b,c);
}catch(e){
if(e===foreach.break)return;
else throw e;
}
}
foreach.break=new Error("StopIteration");
}
3、for in
可以用来遍历数组,也可以遍历对象
var a = {"first":,"second":,"third":};
上面的这种形式就可以用for in的方式遍历
for(var x in a){
alert(a[x]);
}
jQuery的遍历方法的更多相关文章
- jQuery通用遍历方法each的实现
each介绍 jQuery 的 each 方法,作为一个通用遍历方法,可用于遍历对象和数组. 语法为: jQuery.each(object, [callback]) 回调函数拥有两个参数:第一个为对 ...
- JQuery 的遍历方法 $.each
博主呢最近在公司实习,发现公司基本上都会统一代码风格,今天看到还有很多事用JQuery写的js 请求Ajax与后台进行数据交互的方式 当我看到$each 遍历时 然我想起我学JQuery的时候 于是复 ...
- jQuery使用(十一):jQuery实例遍历与索引
each() children() index() 一.jQuery实例遍历方法each() jQuery实例上的each()方法规定要运行的函数,并且给函数传入两个参数:index,element. ...
- jquery中siblings方法配合什么方法一起使用
siblings() 获得匹配集合中每个元素的同胞,通过选择器进行筛选是可选的.接下来通过本文给大家介绍jQuery siblings()用法实例详解,需要的朋友参考下吧 siblings() 获得匹 ...
- jQuery的end() 方法
定义和用法 end() 方法结束当前链条中的最近的筛选操作,并将匹配元素集还原为之前的状态. 语法 .end() 详细说明 大多数 jQuery 的遍历方法会操作一个 jQuery 对象实例,并生成一 ...
- JS数组与对象的遍历方法大全
本文简单解析各种数组和对象属性的遍历方法: 原生for循环.for-in及forEach ES6 for-of方法遍历类数组集合 Object.key()返回键名的集合 jQuery的$.each() ...
- jQuery三——筛选方法、事件
一.jquery常用筛选方法 以下为jquery的常用筛选方法: 代码示例如下: <!DOCTYPE html> <html lang="en"> < ...
- Jquery中each的三种遍历方法
Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...
- JavaScript、jQuery、fish的遍历方法(each、forEach)总结
起因 在工作中,需要在遍历的dom中找到第一个并做下操作然后退出遍历,我首先想到了用each方法,但由于无论是公用的jQuery组件还是公司的fish组件.我都忘记了怎么去退出遍历,所以就有了这篇帖子 ...
随机推荐
- adb开启不了解决方案
原文地址: adb开启不了解决方案 - vaecer - 博客频道 - CSDN.NET http://blog.csdn.net/vaecer/article/details/45894643 ...
- python脚本工具-1 制作爬虫下载网页图片
参考:http://www.cnblogs.com/fnng/p/3576154.html 本文参考虫师的博客“python实现简单爬虫功能”,整理分析后抓取其他站点的图片并下载保存在本地. 抓取图片 ...
- 去掉php框架CI默认url中的index.php
CI默认的rewrite url中是类似这样的 例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样 http://localhost/CodeIgniter/index ...
- eclipse+pydev (python) 配置出错
错误: eclipse+pydev 配置出错,就是在选择python interpreter那一步: See error log for details.com.sun.org.apache.xerc ...
- bzoj1025: [SCOI2009] 游戏 6
DP. 每种排法的长度对应所有循环节长度的最小公倍数. 所以排法总数为和为n的几个数的最小公倍数的总数. #include<cstdio> #include<algorithm> ...
- 软件测试Web数据分析工具HttpWatch安装
1.下载破解版:http://www.pc6.com/softview/SoftView_13845.html 2.安装时引入授权文件并汉化: 3.打开IE9,菜单:管理加载项->工具栏和扩展, ...
- android listview 使用checkbox问题
在android中使用listview时需要了解listview加载数据的原理,为了避免listview由于列表项过多每次需要进行new造成性能低下的问题,android中的listview使用了控件 ...
- ECSHOP:首页实现显示子分类商品,并实现点击Tab页切换分类商品
例子:首页实现显示子分类商品,并实现点击Tab页切换分类商品(非AJAX) 开始: 1. 打开调试开关 文件地址:include/cls_template.php 找到 : functi ...
- windows版本git的下载地址
最后编辑时间 2016年09月01日13:13 首先需要下载msysgit,下载最新版本即可 https://git-for-windows.github.io/ 这个是源代码 https://git ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.6
If $A$ is a contraction, show that $$\bex A^*(I-AA^*)^{1/2}=(I-A^*A)^{1/2}A^*. \eex$$ Use this to sh ...