Javascript立即擦除所有节点。 [英] Javascript Erase all Nodes at once.

查看:68
本文介绍了Javascript立即擦除所有节点。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在以多种选择的方式工作,我正在与几个DOM一起工作。这意味着我需要处理节点及其子节点。

当我在这里找到此链接时,我正在网上搜索从父节点删除所有节点的代码:
删除子节点的最佳方法

我使用并测试了它,但是当删除所有第一个子节点时,我认为它最终会删除父本身或第一个不允许我再次创建子节点的子节点。

所以我再次重新编写了代码,因此在我的情况下它可以更好地工作:

展开 | 选择 | Wrap | 行号

解决方案

当你有一个'空''的最后一个孩子时,这段代码就不会删除任何东西。所以从父母那里移除所有子节点是不可靠的...你在这里链接的代码只是声称的。


假设父节点有一些子节点,其中一个节点只是一个''空'节点甚至是一个文本节点...然后什么都不会被删除。


亲切的问候


的确,但是通过使用像< p>这样的标记包装textnode或空节点,可以很容易地解决它或者< br>或者如果你不想添加任何类型的fomatation,标签< span> ;.

换句话说:

展开 | 选择 | Wrap | 行号

当然你可以包装节点......但是创建或删除节点的每个DOM操作都非常昂贵,所以只需使用更多节点来使脚本工作就不是一个选择。你链接到的脚本就在这里(让它在这里更好地看一下):

展开 | 选择 | Wrap | 行号


I''m working now in a form of multiple choice and i am working with several DOM''s at once. Which means i need to work with nodes and their childs.
I was searching the web for a code that erased all nodes from a parent node when i found this link over here:
Best way to remove child nodes
I used and tested it, but when removing all first child nodes, i think it ends up by removing the parent itself or the first child node which doesn''t me allow to create child nodes again.
So i readapted the code again, so it sould work a little better in my case:

Expand|Select|Wrap|Line Numbers

解决方案

this code just don''t remove anything further when you would have an ''empty'' last child. so it wouldn''t reliable remove all child nodes from a parent ... what the code that you linked here just claims.

suppose the parent has some child nodes and one of them is just an ''empty'' node or even a textnode ... then nothing will be removed.

kind regards


Indeed, but its easily solved by wrapping up the textnode or empty node with a tag like <p> or <br> or if you dont want to add any kind of fomatation, the tag <span>.
For another words:

Expand|Select|Wrap|Line Numbers


of course you could wrap nodes ... but every DOM operation to create or remove nodes is very ''expensive'' so just using more nodes to make a script working isn''t really an option. the script to which you have linked is here (to have it here to look at it better):

Expand|Select|Wrap|Line Numbers


这篇关于Javascript立即擦除所有节点。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆