How do I make this loop all children recursively?

前端 未结 10 610
情书的邮戳
情书的邮戳 2020-12-24 12:06

I have the following:

for (var i = 0; i < children.length; i++){
   if(hasClass(children[i], \"lbExclude\")){
       children[i].parentNode.removeChild(ch         


        
10条回答
  •  梦毁少年i
    2020-12-24 12:54

    If you use a js library it's as simple as this:

    $('.lbExclude').remove();
    

    Otherwise if you want to acquire all elements under a node you can collect them all natively:

    var nodes = node.getElementsByTagName('*');
    for (var i = 0; i < nodes.length; i++) {
      var n = nodes[i];
      if (hasClass(n, 'lbExclude')) {
        node.parentNode.removeChild(node);
      }
    }
    

提交回复
热议问题