Html 使用 getElementsByClassName 选择所有类,然后单击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18784763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:22:33  来源:igfitidea点击:

Select all class's with getElementsByClassName and click

javascriptclasshtml

提问by Max Port

I cant seem to click all of the class's

我似乎无法点击所有课程

document.getElementsByClassName('node closed')[0].click();

This works but will only click on the first class, I need this to click all of the classes named 'node closed'

这有效但只会单击第一个类,我需要它来单击所有名为“节点关闭”的类

Thanks

谢谢

回答by Harry

[0]means only the first element of the node list returned by getElementsByClassName.

[0]表示仅由 返回的节点列表的第一个元素getElementsByClassName

You have to do getElementsByClassNameand iterate through all the matched elements like shown below:

您必须执行getElementsByClassName并遍历所有匹配的元素,如下所示:

var el = document.getElementsByClassName('node closed');
for (var i=0;i<el.length; i++) {
    el[i].click();
}

Working Demo

工作演示

回答by Nishad K Ahamed

iterate the result in a loop and assign click to each elements:

在循环中迭代结果并将点击分配给每个元素:

var list=document.getElementsByClassName('node closed')
for(var i=0;i<list.length;i++){
list[i].click()
}

回答by Shadow

document.getElementsByClassName has some issues in IE

document.getElementsByClassName 在 IE 中有一些问题

use jquery

使用jQuery

window.onload=function(){

$(.yourclass).each(function(){

 $(this).trigger('click');

});

}

回答by Sasidharan

$(".node closed").filter(function() {
    return $(this).click();
});

回答by Ganesh Pandhere

just remove [0] and it will access all matched elements as [0] points to first element only.

只需删除 [0] ,它将访问所有匹配的元素,因为 [0] 仅指向第一个元素。