How would I go about removing all of the child elements of a DOM node in JavaScript?
(我将如何删除JavaScript中DOM节点的所有子元素?)
Say I have the following (ugly) HTML:
(说我有以下(丑陋的)HTML:)
<p id="foo">
<span>hello</span>
<div>world</div>
</p>
And I grab the node I want like so:
(我抓住了我想要的节点,如下所示:)
var myNode = document.getElementById("foo");
How could I remove the children of foo
so that just <p id="foo"></p>
is left?
(我怎样才能删除foo
的子代,只剩下<p id="foo"></p>
?)
Could I just do:
(我可以做:)
myNode.childNodes = new Array();
or should I be using some combination of removeElement
?
(还是应该使用removeElement
某种组合?)
I'd like the answer to be straight up DOM;
(我希望答案直接是DOM;)
though extra points if you also provide an answer in jQuery along with the DOM-only answer.(如果您还提供jQuery答案以及仅DOM答案,则需要加分。)
ask by Polaris878 translate from so