从Nodelist中提取href

时间:2016-10-12 11:50:03

标签: javascript phantomjs

var downloadLinks = document.querySelectorAll('[href*="/Download"]');

给我带有所有元素的NodeList,但是如何将所有节点中的href值作为单个数组提取?

我尝试了return Array.from(downloadLinks),但看起来PhantomJS不支持ES6,所以我得TypeError: undefined is not a function

2 个答案:

答案 0 :(得分:3)

var downloadLinks = document.querySelectorAll('[href*="/Download"]');
var arrHREF = []; // create an Array to save hrefs
var i = 0;
for(; i<downloadLinks.length; i++) {
  arrHREF.push(downloadLinks[i].href); // push hrefs in array
}

或者你可以把它写成一行(使用在执行括号中声明变量的for循环属性)

for (var downloadLinks = document.querySelectorAll('[href*="/Download"]'), arrHREF = [], i = 0; i < downloadLinks.length; i++) arrHREF.push(downloadLinks[i].href);

答案 1 :(得分:0)

Array.prototype.map.call(document.querySelectorAll("[href*=" / Download "]"), function (e) {
  return e.getAttribute('href');
});