jquery选择器返回值

jquery选择器$('selector')返回的不是数组,而是封装好的jquery对象。但这个对象有一个特别的地方,就是查询到的节点被以下标为属性,添加到了jquery对象上,所以它看起来像数组,因为它存在一些属性是数字;

比如如下的代码:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>find/eq</title>
</head>
<style>
    .yellow { background:yellow; }
</style>
<body>
<div class="box">
    <p id="nihaome">你<span>好</span>吗</p>
    <p>我很<span>好</span></p>
</div>
</body>
<script type="text/javascript" src="./static/js/jquery-1.12.4.min.js"></script>
<script>
    $(".box").find("p").eq(1).addClass("yellow");
    $("p").find("span").css('color','red');
    console.log($(".box").find("p"));
    console.log(document.getElementById("nihaome"));
</script>
</html>

console.log($(".box").find("p"))的输出是:


    console.log(document.getElementById("nihaome"))的输出是:

如果需要访问jquery选择器返回的一组对象的某一个,可以使用.eq(index)方法,比如:

$(".box").find("p").eq(1).addClass("yellow");

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值