I have a parent div and a child div. here is the html
我有一个父div和一个子div。这是html
<div id="parent">Parent
<div id="child">Child Div</div>
</div>
The Js
var parent = document.createElement("parent");
var childDiv = document.createElement("child");
parent.removeChild(childDiv);
But it does not remove the child. Please tell me where am i doing it wrong. Thanks
但它并没有删除孩子。请告诉我我在哪里做错了。谢谢
1 个解决方案
#1
createElement()
actually creates new HTML elements -- that is not the same as retrieving elements that already exist.
createElement()实际上创建了新的HTML元素 - 这与检索已存在的元素不同。
To get a reference to your HTML elements, use getElementById()
instead.
要获取对HTML元素的引用,请改用getElementById()。
Try this:
var parent = document.getElementById("parent");
var childDiv = document.getElementById("child");
parent.removeChild(childDiv);
#1
createElement()
actually creates new HTML elements -- that is not the same as retrieving elements that already exist.
createElement()实际上创建了新的HTML元素 - 这与检索已存在的元素不同。
To get a reference to your HTML elements, use getElementById()
instead.
要获取对HTML元素的引用,请改用getElementById()。
Try this:
var parent = document.getElementById("parent");
var childDiv = document.getElementById("child");
parent.removeChild(childDiv);