each() 方法规定为每个匹配元素规定运行的函数。这篇文章主要给大家介绍jquery中each方法的使用详解,需要的朋友参考下吧
概述:
each() 方法规定为每个匹配元素规定运行的函数。
返回 false 可用于及早停止循环,相当于break。
返回 true 可以结束本次循环,相当于continue。
语法:
$(selector).each(function(index,element){ })
index - 选择器的 index 位置
element - 当前的元素(也可使用 "this" 选择器)
$(selector).each(function(){ })
$.each(array,function(Key,Value){ })1.遍历js数组
$(function(){
var array=["aaa","bbb","ccc"];
$.each(array,function(i,j){
alert(i+":"+j); //i表示索引,j代表值
});
})2.遍历Object对象
var obj = new Object();
obj.name="zs";
$.each(obj, function(name, value) {
alert(this); //this指向当前属性的值,等价于value
alert(name); //name表示Object当前属性的名称
alert(value); //value表示Object当前属性的值
});3.遍历JSON对象
var json ={"name":"zhangSan","role":"student"};
$.each(json,function(key,value){
alert(key+":"+value);
});4.遍历由多个JSON对象组成的数组
var json =[{"name":"Amy","role":"student"},{"name":"Tom","role":"student"}];
$.each(json, function(index, value) {
alert("index="+index+"\n" +"name:"+value.name+"\n"+"role:"+value.role+"\n");
});5.遍历jQuery对象
<head>
<meta charset="utf-8" />
<title>遍历jQuery对象</title>
<script src="js/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(function(){
$("input[type='button']").bind("click",function(){
$("li").each(function(){
alert($(this).text())
});
});
});
</script>
</head>
<body>
<input type="button" value="触发事件"/>
<ul>
<li>first</li>
<li>second</li>
</ul>
</body>上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
以上就是jQuery中each方法的使用详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号