当前位置:网站首页>How JS correctly clears all child elements under an element
How JS correctly clears all child elements under an element
2022-06-30 19:24:00 【JA+】
The following code , Deleting child nodes through traversal can cause problems
const userList = document.getElementById('userList');
for (let i = 0; i < userList.children.length; i++) {
const element = userList.children[i];
element.remove();// userList.removeChild(element);
}It seems very intuitive .
in fact ,
call element.remove() or parentNode.removeChild(element) After deleting this element .
Original array (HTMLCollection) The size has changed !
This results in incomplete deletion of child elements .
because element.children The value of is HTMLCollection (MDN) object , Is a class array object . You can't traverse and delete the array .
So when you delete all the child elements , Each traversal HTMLCollection Delete the first value of the object .
Code
let ele;
while ((ele = userList.children.item(0))) {
ele.remove();
}
// or
while ((ele = userList.firstChild)) {
ele.remove();
}Use here HTMLCollection.item() To get the first 1 Elements . You can also use an array to get .element.children[0]
Now that you get the first element , There's a simpler way element.firstChild. The same goes for the last element element.lastChild ( This is also the code used by many schemes on the Internet )
firstChild / lastChild The return is Node type , Use firstElementChild 、lastElementChild Come back to Element type .
as for Node and Element What's the difference , Please search by yourself .
PS
This is why our contemporary front-end relies too much on the front-end framework for operation dom, And the original js Knowledge loopholes .
边栏推荐
- Rust 如何实现依赖注入?
- Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched
- Ditto设置全局仅粘贴文本快捷键
- 基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务
- 拓维信息使用 Rainbond 的云原生落地实践
- ros advertise 发布数据小技巧--latch配置
- Year after year, why is breaking the data island still the primary task of enterprise development
- mysql修改数据类型_MySQL修改字段类型[通俗易懂]
- The cloud native landing practice of using rainbow for Tuowei information
- Full recharge, im+rtc+x full communication service "feedback season" starts
猜你喜欢

The cloud native landing practice of using rainbow for Tuowei information

【DesignMode】工厂模式 (factory pattern)

Browser window switch activation event visibilitychange

商业智能BI与业务管理决策思维之四:业务成本分析

Kalman filter -- Derivation from Gaussian fusion

Swin-Transformer(2021-08)

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

如何使用物联网低代码平台进行服务管理?

ArcGIS无插件加载(无偏移)天地图

嵌入式软件开发新趋势:DevOps
随机推荐
Opencv data type code table dtype
「经验」爬虫在工作中的实战应用『理论篇』
DTD modeling
20200525 Biotechnology - Sichuan Normal University self taught Biotechnology (undergraduate) examination plan txt
法国A+ 法国VOC标签最高环保级别
拓维信息使用 Rainbond 的云原生落地实践
Unity技术手册-初探性能优化
MySQL 索引测试
【DesignMode】单例模式(singleton pattern)
Courage to be hated: Adler's philosophy class: the father of self inspiration
Go redis connection pool
nats集群部署
NBI visual platform quick start tutorial (V) introduction to editor functions and operations
com.alibaba.fastjson.JSONObject # toJSONString 消除循环引用
一文详解|Go 分布式链路追踪实现原理
「杂谈」如何改善数据分析工作中的三大被动局面
20200525-生物技术-四川师范大学自考生物技术(本科)考试计划.txt
sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户
Rust 如何实现依赖注入?
Evolution of screen display technology