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方法的使用的更多相关文章
- jquery 中一些 特殊方法 的特殊使用 一览表
cnblogs的页面, 一种是管理页面, 是随笔的列表 a full list of essays. 另一种是 首页. 要搜索文档的话, 就使用 "首页"的那种方式. 一个jque ...
- 解决关于jquery中$.get()方法总是报“HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy”错的方法
解决关于jquery中$.get()方法总是报“HierarchyRequestError: Node cannot be inserted at the specified point in the ...
- jQuery中的join方法
和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...
- jquery中的index方法
问题描述:灵活使用jquery中的index方法 方法签名:index([selector|element]) 用法概述:P1.index(P2) //调用者P1可以为对象或集合 参数为空,返回P1 ...
- 用JQuery中的Ajax方法获取web service等后台程序中的方法
用JQuery中的Ajax方法获取web service等后台程序中的方法 1.准备需要被前台html页面调用的web Service,这里我们就用ws来代替了,代码如下: using System; ...
- jQuery中的ready方法及实现按需加载css,js
模拟jQuery中的ready方法及实现按需加载css,js 一.ready函数的实现 经常用jQuery类库或其他类库中的ready方法,有时候想想它们到底是怎么实现的,但是看了一下jQuery中的 ...
- $.ajax()方法详解 jquery中的ajax方法
jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...
- jQuery中的$.ajax()方法
jQuery中的$.ajax()方法 $.ajax({ type:"POST", url:"../page/user.action?userId=" + use ...
- jquery中的ajax方法参数
引用来自:http://www.cnblogs.com/tylerdonet/p/3520862.html jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String ...
- HTML5中的data-*属性和jQuery中的.data()方法使用
原文地址链接:http://blog.csdn.net/fly_zxy/article/details/50687691: HTML5中的data-*属性 我们往往会根据需要在HTML标记上添加自定义 ...
随机推荐
- toFixed()精度丢失;复选框全选、取消
一.精度丢失和重写toFixed()函数 1.重写toFixed() Number.prototype.toFixed = function(length){ var carry = 0; //存放进 ...
- CDH 报错:under replicated blocks
1.刚安装好CDH5.16.1集群(2个节点)出现了under replicated blocks错误,如下图 2.原因是CDH默认文件备份3份,而我们只有2节点,所以解决方法如下: ①集群增加一个新 ...
- spring boot中利用mybatis-generator插件生成代码
使用Idea在spring boot中集成mybatis-generator,自动生成mapper.xml model dao 文件 一.配置 pom.xml 在pom.xml的<plugi ...
- 【Code Tools】Java微基准测试工具JMH之入门篇
一.JMH是什么 JMH是一个Java工具,用于构建.运行和分析用Java和其他语言编写的以JVM为目标的 nano/micro/milli/macro 基准测试. 二.基本注意事项 1)运行JMH基 ...
- C语言实验一(1)
#include<stdio.h>int main(){ int a,b,sum; a=123; b=456; sum=a+b; printf("sum is %d\n" ...
- js如何实现类的继承
方法一:借助构造函数实现继承 这种方法的缺点:原型链上的东西并没有被继承. 方法二:借助原型链实现继承 这种方法的缺点:改变了一个实例对象,另一个实例对象也跟着改变,因为s1.__proto__ == ...
- 在webpack3里使用uglifyjs
环境:webpac<4的场景下,安装uglifyjs. cnpm install uglifyjs-webpack-plugin -D 安装完毕后,去npm里查看uglifyjs的使用方法并添加 ...
- swoole框架和其所涉及的协议
网络知识基础 OSI协议 :应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 tcp通信 TCP通信过程包括三个步骤:建立TCP连接通道,传输数据,断开TCP连接通道(三次握手.四次挥手). ...
- strtol详解
文章装载自:http://blog.csdn.net/happylife1527/article/details/8441799,大家看到这篇文章想转载的请注明出处,尊重原作者劳动成果. 今天,在re ...
- (04) springboot 下的springMVC和jsp和mybatis
1. springboot 和springmvc下的注解完全一样(新增了一些有用的) 常用的注解如下: @Controller @RestController= @Controller + @Resp ...