Removing elements by class name?

后端 未结 16 1755
猫巷女王i
猫巷女王i 2020-11-27 12:06

I have the below code to find elements with their class name:

// Get the element by their class name
var cur_columns = document.getElementsByClassName(\'colu         


        
16条回答
  •  一个人的身影
    2020-11-27 12:50

    The skipping elements bug in this (code from above)

    var len = cells.length;
    for(var i = 0; i < len; i++) {
        if(cells[i].className.toLowerCase() == "column") {
            cells[i].parentNode.removeChild(cells[i]);
        }
    }
    

    can be fixed by just running the loop backwards as follows (so that the temporary array is not needed)

    var len = cells.length;
    for(var i = len-1; i >-1; i--) {
        if(cells[i].className.toLowerCase() == "column") {
            cells[i].parentNode.removeChild(cells[i]);
       }
    }
    

提交回复
热议问题