当前位置: 移动技术网 > IT编程>开发语言>Jquery > jQuery eq()

jQuery eq()

2018年01月17日  | 移动技术网IT编程  | 我要评论

获取匹配元素集上对应索引的元素。

返回值是一个jQuery对象。

语法结构一:

$(selector).eq(index)

参数解析:

index:一个整数,指示元素的位置,以0为基数。

jQuery1.12版本添加。

语法结构二:

$(selector).eq( -index )

参数解析:

index:一个整数,指示元素的位置,从集合中的最后一个元素开始倒数。

jQuery1.4版本添加。

代码实例:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<style>
ul {
  list-style:none;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("li").eq(1).css("color","blue")
})
</script>
</head>
<body>
<div>
  <ul>
    <li>蚂蚁部落一</li>
    <li>蚂蚁部落二</li>
    <li>蚂蚁部落三</li>
    <li>蚂蚁部落四</li>
  </ul>
</div>
</body>
</html>

把索引是1的li元素中的字体颜色设置为蓝色。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<style>
ul {
  list-style:none;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("li").eq(-2).css("color","blue")
})
</script>
</head>
<body>
<div>
  <ul>
    <li>蚂蚁部落一</li>
    <li>蚂蚁部落二</li>
    <li>蚂蚁部落三</li>
    <li>蚂蚁部落四</li>
  </ul>
</div>
</body>
</html>

索引是负数的时候,从最后一个元素往回计数。

原文是jQuery eq()

更多jquery内容查看jQuery教程板块。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网