jquery获取所有同类标签不能输出

html

<dd class="col-sm-11">
    <a href="http://beijing.example.com">北京</a>
    <a href="http://tianjin.example.com">天津</a>
    <a href="http://shanghai.example.com">上海</a>
    <a href="http://chongqing.example.com">重庆</a>
</dd>

我想把所有链接提取出来:

<script>
    $(function () {
        var hrefs = $('a').attr("href");
        console.log(hrefs);
    });

</script>

浏览器控制台没有显示任何东西,要怎么才能打印?

阅读 1.8k
2 个回答
 $('.col-sm-11 a').each(function(i,e){
                    console.log($(this).attr("href"))
                  });

应该是打出来了一个吧。
jquery对dom数组取值的时候只会获取第一个dom的属性。
所以遍历输出吧。