如何迭代jquery选择器的结果 [英] how to iterate a result of jquery selector

查看:71
本文介绍了如何迭代jquery选择器的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to iterate a result of query selector.

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时

when I use javascript

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

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

the result is the tag a href attribute I don't know why.

推荐答案

使用 $。每个

$("#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

这篇关于如何迭代jquery选择器的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆