微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何迭代jquery选择器的结果

我想迭代查询选择器的结果.

Html代码

<nav id="navigation">
        <a href="#" tabindex="1" class="active_nav">nav1</a>
        <a href="#" tabindex="2">nav2</a>
        <a href="#"tabindex="3">nav3</a>
</nav>

当我使用javascript

alert($("#navigation >a")[0]);

结果是标签是href属性
我不知道为什么

解决方法

使用$.each
$("#navigation > a").each(function() {

     console.log(this.href)
});
$('#navigation > a')[0]
      ^              ^---- Selects the 1st dom object from the jQuery object
      |                    that is nothing but the index of the element among 
      |                    the list of elements
      |-------  Gives you children of nav(3 anchor tags in this case)  which is a
                jQuery object that contains the list of matched elements

原文地址:https://www.jb51.cc/jquery/151090.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐