如何获取<a>

时间:2017-10-15 19:35:40

标签: jquery html

How to get all href attributes values of

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

How can I get Href attributes values in one array?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:-1)

&#13;
&#13;
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)
&#13;
<ul>
   <li class="demo">
        <a href="/chat/2"></a>
   </li>
   <li class="demo">
        <a href="/chat/3"></a>
   </li>
</ul>
&#13;
&#13;
&#13;