简体   繁体   English

迭代document.getElementsByClassName

[英]iterating document.getElementsByClassName

Hi guys i'm trying to iterate a getElementsByClassName in this way: 嗨,大家好,我正在尝试以这种方式迭代getElementsByClassName:

var i;
for (i = 0; i < 20; i++) { 
if (document.getElementsByClassName("available")[i]==="undefined"){}
else{
 document.getElementsByClassName("available")[i].click();  }



}

if i do it in chrome console it works fine , but when i run it trough my chrome extension it says read property 'click' of undefined , do you know how i can solve? 如果我在chrome控制台中执行它,则工作正常,但是当我通过chrome扩展程序运行它时,它显示未定义的read属性'click',您知道如何解决吗?

Try something like this 试试这个

 var available = document.getElementsByClassName("available"); //collection of elements matching the query //Loop collection. //Skips loop if the available collection is empty for (var i = 0; i < available.length; i++) { available[i].click(); } 

Your if statement isn't working as you're not checking to see whether the type of the target element is undefined. 您的if语句不起作用,因为您没有检查目标元素的类型是否未定义。 Try this instead - 试试这个-

document.getElementsByClassName("available")[i]===undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM