jQuery 遍历 中 eq() 方法
来源:互联网 发布:和服羽织淘宝 编辑:程序博客网 时间:2024/05/16 13:49
在写js的时候很多情况下都需要遍历,jquery中提供了eq()的方法。
1、通过为 index 为 2 的 div 添加适当的类,将其变为蓝色:
$("body").find("div").eq(2).addClass("blue");
示例:
<!DOCTYPE html><html><head> <style> div { width:60px; height:60px; margin:10px; float:left; border:2px solid blue; } .blue { background:blue; } </style> <script type="text/javascript" src="/jquery/jquery.js"></script></head><body> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <script>$("body").find("div").eq(2).addClass("blue");</script></body></html>
$('li').eq(2).css('background-color', 'red');
示例:
<!DOCTYPE html><html><head> <style> div { width:60px; height:60px; margin:10px; float:left; border:2px solid blue; } .blue { background:blue; } </style> <script type="text/javascript" src="/jquery/jquery.js"></script></head><body><ul> <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> <li>list item 4</li> <li>list item 5</li></ul><script>$('li').eq(2).css('background-color', 'red');</script></body></html>
3、如果提供负数,则指示从集合结尾开始的位置,而不是从开头开始,倒数第二个背景色变成红色
$('li').eq(-2).css('background-color', 'red');
示例:
<!DOCTYPE html><html><head> <style> div { width:60px; height:60px; margin:10px; float:left; border:2px solid blue; } .blue { background:blue; } </style> <script type="text/javascript" src="/jquery/jquery.js"></script></head><body><ul> <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> <li>list item 4</li> <li>list item 5</li></ul><script>$('li').eq(-2).css('background-color', 'red');</script></body></html>
4、如果无法根据指定的 index 参数找到元素,则该方法构造带有空集的 jQuery 对象,length 属性为 0
$('li').eq(5).css('background-color', 'red');
这里,没有列表项会变为红色,这是因为 .eq(5) 指示的第六个列表项。
阅读全文
0 0
- jQuery 遍历 中 eq() 方法
- jQuery 遍历 - eq() 和siblings() 方法
- jquery循环遍历中add\each\andSelf\eq\filter\map\slice方法的使用
- jQuery中eq()方法用法实例
- JQuery中的eq()方法
- jQuery的eq()方法
- jquery中.eq()与:eq()的区别
- html---jquery的eq()方法
- 使用jquery 遍历函数-find()、eq()
- jQuery中eq()的使用
- jquery中eq和get
- jquery中eq和get (
- jquery中.index()和.eq()
- jquery中eq和get
- jQuery 遍历 - 过滤---first--last---eq--not--filter方法(二十五)
- jquery eq
- jQuery中eq和get的区别
- jQuery中eq和get的区别
- 智能语音质检系统如何做到深度跟踪分析客户
- Spring Boot -- Spring Data Jpa(2)
- 上下文切换
- ansible ssh免密碼登錄
- 阿里云服务器Linux(Ubuntu系统)安装tomcat以及外网访问详解
- jQuery 遍历 中 eq() 方法
- NTP协议与计算
- 优秀网页设计
- Android SDK下载安装及配置教程
- 简单的事件分发机制
- 开源地图MapBox自定义(一):搭建环境
- codeforces 864D 莫比乌斯反演
- php学习笔记(二十三)PDO的预处理语句的查询以及结果集的使用
- sql,with as ,in的配合使用