博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dom 删除所有子元素_如何从DOM元素中删除所有子级
阅读量:2513 次
发布时间:2019-05-11

本文共 833 字,大约阅读时间需要 2 分钟。

dom 删除所有子元素

Given an item in the DOM, use to identify it, like this:

给定DOM中的项目,请使用进行标识,如下所示:

const item = document.querySelector('#itemId')

then, to remove all its children elements, you have a few different solutions.

然后,要删除其所有子元素,您有几种不同的解决方案。

The fastest way looks like this:

最快的方法如下所示:

item.innerHTML = ''

Another solution that is suggested is this: create a loop, check if the firstChild property is defined (the element has at least a child) and then remove it:

建议的另一种解决方案是:创建一个循环,检查是否已定义firstChild属性(该元素至少具有一个孩子),然后将其删除:

const item = document.querySelector('#itemId')while (item.firstChild) {  item.removeChild(item.firstChild)}

The loop ends when all children are removed.

删除所有子级后,循环结束。

The first, in most performance benchmarks I checked, looks like being the fastest solution.

在我检查的大多数性能基准测试中,第一个似乎是最快的解决方案。

翻译自:

dom 删除所有子元素

转载地址:http://ohqgb.baihongyu.com/

你可能感兴趣的文章
【poj1009】 Edge Detection
查看>>
【spoj LCS2】 Longest Common Substring II
查看>>
去掉PowerDesigner生成SQL脚本中字段名带的引号
查看>>
win10操作系统安装oracle11g时出现不满足最低配置的操作INS13001
查看>>
中文乱码问题(页面乱码,eclipse乱码,请求响应乱码)
查看>>
extern使用方法总结!(转)
查看>>
HTTP请求过程详解
查看>>
【C#】wpf中的xmlns命名空间为什么是一个网址,代表了什么意思(转载)
查看>>
你不是迷茫,你是自制力不强
查看>>
【转载】setContentView和inflate区别
查看>>
Bootstrap栅格系统
查看>>
自然语言工程师要求
查看>>
Leetcode 452.用最少数量的箭引爆气球
查看>>
【转】Linux设备驱动之Ioctl控制
查看>>
实例说明>
查看>>
日常理财
查看>>
ng-bind-html在ng-repeat中问题的解决办法
查看>>
制定document.getElementByClassName()
查看>>
UVA-315 无向图求割点个数
查看>>
Python学习_4_购物车
查看>>