javascript - 从 Nodelist 中提取 href

标签 javascript phantomjs

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/

相关文章:

javascript - 具有相同类的多个 div 的特定 div 的 CSS

javascript - 使用变量重新加载网页

javascript - 迭代 Angular 12 中接口(interface)定义的属性?

node.js - 如何使用 nightjs 正确停止 phantomjs 实例

angularjs - 当指令是属性时单元测试失败

python - 在 Python 中通过 Selenium 模拟 PhantomJS 上的 HTML5 视频支持

javascript - 我可以动态地将一个模块注入(inject)另一个模块吗?

javascript - onclick 和 href 无法在移动设备上协同工作

testing - 如何检查是否使用 PhantomJS 触发了 HTML5 验证?

ubuntu - 如何设置 phantomjs 默认字体?