我怎么能得到与jQuery计数链接的href值

How can I get the href value from counted links with jQuery

本文关键字:链接 href jQuery 怎么能      更新时间:2023-09-26

HTML源代码:

<a href="http://asd.com/123/qwe">1</a>
<a href="http://asd.net/asd">2</a>
<a href="http://123.com">3</a>
<a href="http://www.a1s2d3.com">4</a>
<a href="http://www.q1w2e3.com">5</a>
<a href="http://bnm.org/questions/">6</a><br /><br />
<span id="element"></span>

jQuery代码
var total = $('a').size();
$('#element').text(total);

我得到一个标签的大小。但我不能得到所有标签的href值。我怎么写循环呢?

你只需要这个:

 $('#element').text(function(){
   return $('a').map(function(){ return this.href }).get().join(","); 
 });
基本上

var arrOfLinks = $('a').map(function(){ return this.href }).get();

将为您提供一个链接数组。