1、选择器+遍历

$('div').each(function (i){

i就是索引值

this 表示获取遍历每一个dom对象

});

2、选择器+遍历

$('div').each(function (index,domEle){

index就是索引值

domEle 表示获取遍历每一个dom对象

});

3、更适用的遍历方法

1)先获取某个集合对象

2)遍历集合对象的每一个元素

var d=$("div");

$.each(d,function (index,domEle){

d是要遍历的集合

index就是索引值

domEle 表示获取遍历每一个dom对

});

案例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>属性选择器学习</title>

<script language="JavaScript" type="text/javascript" src="../include/jQuery.js"></script>

<script language="javascript" type="text/javascript">

//使用jquery加载事件

$(document).ready(function (){

//<input id="btn0" type="button" value="利用jquery对象实现 修改表单中可见的文本域的值  $('input[type=text]:enabled')" />

$("#btn0").click(function (){

//当点击按钮后,设置id=two的div的背景颜色是绿色

$("input[type=text]:enabled").each(function(index,domEle){

//domEle.value="xxxxx";

$(domEle).val("xxxxxxxx");

});

});

//<input id="btn1" type="button" value="利用jquery对象实现 修改表单中不可修改的文本域的值  $('input[type=text]:disabled')" />

$("#btn1").click(function (){

//当点击按钮后,设置id=two的div的背景颜色是绿色

$("input[type=text]:disabled").each(function(index,domEle){

//domEle.value="xxxxx";

$(domEle).val("xxxxxxxx");

});

});

//<input id="btn2" type="button" value="利用jquery对象实现 获取选中的复选框的个数 $('input[type=checkbox]:checked')" />

$("#btn2").click(function (){

//当点击按钮后,设置id=two的div的背景颜色是绿色

alert($("input[type=checkbox]:checked").length);

/*

$("input[type=checkbox]:checked").each(function(index,domEle){

//alert(domEle.value);

//alert(this.value);

//alert($(this).val());

});

*/

});

//<input id="btn3" type="button" value="利用jquery对象实现 获取选中的下拉菜单的内容  $('select option:selected')" />

$("#btn3").click(function (){

//当点击按钮后,设置id=two的div的背景颜色是绿色

$("select option:selected").each(function(index,domEle){

//domEle.value="xxxxx";

alert($(domEle).text());

});

});

});

</script>

</head>

<body>

<form method="post" action="">

<input type="text" value="可见元素1"  />

<input type="text" value="不可见元素1" disabled="disabled"  />

<input type="text" value="可见元素2"  />

<input type="text" value="不可见元素2"  disabled="disabled" /><br>

<input type="checkbox" value="美女" />美女

<input type="checkbox" value="美男" />美男

<input type="checkbox" value="大爷" />大爷

<input type="checkbox" value="大妈" />大妈

<br>

<input type="radio" value="男" />男

<input type="radio" value="女" />女

<br>

<select id="zhiwei" size="5" multiple="multiple">

<option>PHP开发工程师</option>

<option>数据库管理员</option>

<option>系统分析师</option>

<option>保安</option>

</select>

<select id="xueli">

<option>大专</option>

<option>中专</option>

<option>小学</option>

</select>

</form>

<div style="clear:both;">

<input id="btn0" type="button" value="利用jquery对象实现 修改表单中可修改的文本域的值  $('input[type=text]:enabled')" /><br>

<input id="btn1" type="button" value="利用jquery对象实现 修改表单中不可修改的文本域的值  $('input[type=text]:disabled')" /><br>

<input id="btn2" type="button" value="利用jquery对象实现 获取选中的复选框的个数 $('input[type=checkbox]:checked')" /><br>

<input id="btn3" type="button" value="利用jquery对象实现 获取选中的下拉菜单的内容  $('select option:selected')" /><br>

</div>

</body>

</html>

jq 遍历 each方法的更多相关文章

  1. jq的$.each()方法

    jq的$.each()方法: 语法:jQuery.each(object, [callback]) 回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内容.如果需要退出 each ...

  2. 通过cookie实现搜索框内容保存关闭浏览器之前的操作、jq js实现方法

    jq实现的方法: jq需要在页面中引入JQ.cookie插件 这是一个超轻量级插件 要实现的效果: 下面是jq代码: $(function(){ var til=$("#orderInfoC ...

  3. jQuery 遍历 - parent() 方法

    ylbtech-jQuery-sizzle:jQuery 遍历 - parent() 方法  parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的. 1.A,jQuer ...

  4. java集合类遍历删除方法测试以及使用场景记录

    package test0; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java. ...

  5. iOS开发之使用block块进行数据遍历的方法

    看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...

  6. Map<String, String>循环遍历的方法

    Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 下 ...

  7. jQuery 遍历 - eq() 方法 查找当前元素

    jQuery 遍历 - eq() 方法 if(data[i].status !='已送达'){ $('.w-beget').eq(i).attr('disabled','disabled'); }

  8. jsp c标签不遍历的方法

    生产中遇到过不需要遍历的事情. 一般遍历必须要 <c:forEach items="${resultObj.xxx}" var="data" varSta ...

  9. JQ的ready()方法与window.onload()的区别与联系

    JQ的ready()与window.onload()方法都是在文档加载完毕之后才会被触发的方法,但它们之间的区别也是很明显的. 1.区别与联系:   $(document).ready() windo ...

随机推荐

  1. BZOJ1113 Poi2008 海报PLA【单调栈】【水】

    BZOJ1113 Poi2008 海报PLA Description N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. Input 第一行给出数字N,代表有N个矩形.N在[1,250 ...

  2. BZOJ1087 SCOI2005 互不侵犯King 【状压DP】

    BZOJ1087 SCOI2005 互不侵犯King Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附 ...

  3. hdu 2461 Rectangles

    求矩形的并 矩形个数 1...20m次询问 回答要求的r个矩形的并容斥原理dfs优化: 遇到面积交为0时 这个dfs分支可以不下去了 #include <iostream> #includ ...

  4. LeetCode 360. Sort Transformed Array

    原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...

  5. 使用实例 ---- 使用NUnit在.Net编程中进行单元测试

    [---  资料是从免费网站上获取的,上载在这里,只为交流学习目的,文章原作者保留所有权力,如本博客的内容侵犯了你的权益,请与以下地址联系,本人获知后,马上删除.同时本人深表歉意,并致以崇高的谢意!e ...

  6. 19.Selenium+Python生成测试报告

    1.代码如下所示: from selenium import webdriver import unittest import HTMLTestRunner class BaiduSearch(uni ...

  7. 队列之blah集合

    做了一个NOI上面的问题,叫blah集合,以a为基数,则2x+1和3x+1都在集合中,且集合中全部元素都由此计算得来.a∈[1,50],问升序排列后第n(n∈[1,1000000])个元素是多少.以输 ...

  8. python-redis-pipe文件

    redis导入数据比较头疼的事情,涉及几千万,导入还是很耗时,通过生成pipe文件的方式比较快捷. python3.6.1版本 在linux环境下运行 with open("data1&qu ...

  9. Java将对象写入文件读出——序列化与反序列化

    Java类中对象的序列化工作是通过ObjectOutputStream和ObjectInputStream来完成的. 写入: File aFile=new File("e:\\c.txt&q ...

  10. 1006 Sign In and Sign Out (25)(25 分)思路:普通的时间比较题。。。

    1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...