为简单起见,我目前正在使用javascript。如何获取href值并将其存储在javascript变量中。我需要提取每个课程部分以及每个部分中每个项目的所有属性href值。

<ul class="curriculum-items-list">
 <li class="curriculum-section-container ng-scope">
  <ul>
   <li class="curriculum-item-container ng-scope">
    <a href="http://www.mywebsite.com/videoSec1_1.mp4">
   </li>
   <li class="curriculum-item-container ng-scope">
    <a href="http://www.mywebsite.com/videoSec1_2.mp4">
   </li>
  </ul>
 </li>
 <li class="curriculum-section-container ng-scope">
  <ul>
   <li class="curriculum-item-container ng-scope">
    <a href="http://www.mywebsite.com/videoSec2_1.mp4">
   </li>
   <li class="curriculum-item-container ng-scope">
    <a href="http://www.mywebsite.com/videoSec2_2.mp4">
   </li>
  </ul>
 </li>
</ul>


愿望结果

http://www.mywebsite.com/videoSec1_1.mp4
http://www.mywebsite.com/videoSec1_2.mp4
http://www.mywebsite.com/videoSec2_1.mp4
http://www.mywebsite.com/videoSec2_2.mp4

最佳答案

您可以使用querySelectorAll获取对a元素的引用,然后使用Array.prototype.map将相关数据提取到数组中:

var hrefs = [].map.call(document.querySelectorAll(".curriculum-item-container a"), function (elem) {
    return elem.href;
});


这是working example

09-19 07:41