Select all class's with getElementsByClassName and click

后端 未结 5 1948
不思量自难忘°
不思量自难忘° 2020-12-10 00:19

I cant seem to click all of the class\'s

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

This works but will only click on th

相关标签:
5条回答
  • 2020-12-10 00:23

    document.getElementsByClassName has some issues in IE

    use jquery

    window.onload=function(){
    
    $(.yourclass).each(function(){
    
     $(this).trigger('click');
    
    });
    
    }
    
    0 讨论(0)
  • 2020-12-10 00:28

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

    0 讨论(0)
  • 2020-12-10 00:30

    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()
    }
    
    0 讨论(0)
  • 2020-12-10 00:30
    $(".node closed").filter(function() {
        return $(this).click();
    });
    
    0 讨论(0)
  • 2020-12-10 00:49

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

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

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

    Working Demo

    0 讨论(0)
提交回复
热议问题