2017-10-15 30 views
0

值如何得到所有HREF属性如何得到所有HREF属性<a>

<ul> 
    <li class="demo"> 
     <a href="/chat/2"></a> 
    </li> 
    <li class="demo"> 
     <a href="/chat/3"></a> 
    </li> 
</ul> 

值怎样才能HREF在一个阵列中的属性值?

+0

看起来像其他几个问题进行欺骗: https://stackoverflow.com/questions/7759823/jquery-get-the-href -attribute-for-each-link-in-a-section-of-html https://stackoverflow.com/questions/471606/get-all-hrefs-as-an-array-in-jquery 还有其他几个。 – kmoser

+0

请注意,标记为副本的[这个回答](https://stackoverflow.com/a/14359001/519413)是最好的一个,因为它使用'map()',而不是具有最高票数的那个。 –

回答

0

var Arr=[]; 
 
$("li.demo").each(function(){ 
 
Arr.push($(this).find("a").attr("href")); 
 
}); 
 
console.log(Arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul> 
 
    <li class="demo"> 
 
     <a href="/chat/2"></a> 
 
    </li> 
 
    <li class="demo"> 
 
     <a href="/chat/3"></a> 
 
    </li> 
 
</ul>

+0

如果有用,请检查确认帮助他人 –

-1

let foo = document.getElementsByTagName("a"); 
 
console.log(foo) 
 
let my_array = []; 
 

 
for(let i=0;i<foo.length;i++) { 
 
    my_array.push(foo.item(i).href); 
 
} 
 

 
console.log(my_array)
<ul> 
 
    <li class="demo"> 
 
     <a href="/chat/2"></a> 
 
    </li> 
 
    <li class="demo"> 
 
     <a href="/chat/3"></a> 
 
    </li> 
 
</ul>

相关问题