[英]I have a loop in the Javascript console that is not working. A
I have a loop that I want to use to print all element with a specific class name from a webpage.我有一个循环,我想用它来打印网页中具有特定 class 名称的所有元素。
var arr = [];
for (let i = 0; i < 15; i++) {
document.getElementsByClassName("wM6scouPXXsFDSZmZPHRo DjcdNGtVXPcxG0yiFXIoZ _23wugcdiaj44hdfugIAlnX”)[i].href
}
This code only prints the last element.此代码仅打印最后一个元素。 I have also tried adding a variable and printing it with console.log, it will return an Undefined.
我也试过添加一个变量并用 console.log 打印它,它会返回一个未定义的。
Maybe the URL I am working with is blocking my requests?也许我正在使用的 URL 阻止了我的请求? It only shows the last element and not the one from 1-15.
它只显示最后一个元素而不是 1-15 中的那个。
Run this in developer tools of stackoverflow page.在 stackoverflow 页面的开发人员工具中运行它。 getElementsByClassName will get all the elements with same class. It will be an array of elements, loop over each element and access href.
getElementsByClassName 将获取具有相同 class 的所有元素。它将是一个元素数组,遍历每个元素并访问 href。
const links = document.getElementsByClassName('question-hyperlink'); console.log(links) for (let i = 0; i < links.length; i++) { console.log(links[i].href) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.