var downloadLinks = document.querySelectorAll('[href*="/Download"]');
为我提供了包含所有元素的 NodeList,但我如何从所有节点中仅提取 href
值作为一个数组?
我试过 return Array.from(downloadLinks)
但看起来 PhantomJS 不支持 ES6 所以我得到 TypeError: undefined is not a function
最佳答案
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);
关于javascript - 从 Nodelist 中提取 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39997890/