简体   繁体   English

我在 Javascript 控制台中有一个循环不起作用。 一种

[英]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.

相关问题 javascript if else 语句不起作用。 即使我单击 twoStory 图像,它也会返回 singleStory。 我试过了!=和--- - javascript if else statement not working. It returns singleStory even if I click on the twoStory image. I have tried != and --- 此 JavaScript 代码不起作用。 我的语法错误还是其他原因? - This JavaScript code is not working. Have I got the syntax wrong or is it something else? JavaScript计算不起作用。 - Javascript calculation not working. Javascript 不工作。 无输出 - Javascript not working. No output 我试图在普通的 JavaScript 中实现 useState 挂钩以查看其工作。 下面是我的代码,但它不工作并进入无限循环 - I was trying to implement useState hook in plain JavaScript to see its working. Below is my code, but it is not working and goes into infinite loop JavaScript计算器无法正常工作。 - Javascript calculator not working. Javascript appendChild()无法正常工作。 - Javascript appendChild() not working. Javascript 我在 while 循环中有一个嵌套的 for 循环,但它没有按预期工作 - Javascript I have a nested for loop in a while loop and it's not working as expected yii2 javascript无法正常工作。 我如何添加JavaScript - yii2 javascript is not working. how can i add javascript Javascript_Include_Tag 不工作。 我如何解决它? - Javascript_Include_Tag Not Working. How do I fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM