Removing HTMLCollection elements from the DOM

前端 未结 5 1065
孤独总比滥情好
孤独总比滥情好 2020-11-28 12:53

I have a collection of paragraph elements. Some are empty and some contain whitespace only, while others have content:

Pellentesque habitant morbi t

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 13:25

    The length of your HTMLCollection changes when you remove an item. A way to do it is to use a while loop

    while(paragraphs.length > 0) {
       paragraphs[0].remove();
    }
    

提交回复
热议问题