javascript 如何使用javascript删除子div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19266723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove child div using javascript
提问by Bhargavi
this my html
这是我的 html
<div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
this my script
这是我的脚本
var list=document.getElementById("div2");
list.removeChild("div2""));
when i click the buttton i need to remove the child div(div2) how to do that. using this code i am facing problem please tell. help me . we have any other solution for this problem
当我单击按钮时,我需要删除子 div(div2) 如何做到这一点。使用此代码我遇到了问题,请告诉。帮我 。对于这个问题,我们有任何其他解决方案
回答by Sam Braslavskiy
you have double quotes and double braces at the end. And I'm not sure what you're trying to do. If you'd like to remove element with the id "div2", use:
最后有双引号和双大括号。而且我不确定你要做什么。如果您想删除 id 为“div2”的元素,请使用:
var list=document.getElementById("div2");
list.parentNode.removeChild(list);
回答by Satpal
回答by Gibolt
Use modern JS!
使用现代JS!
const div = document.getElementById("div2");
div.remove();
or just
要不就
document.getElementById("div2").remove()
回答by Vinay Pratap Singh
try removing the elements like this
尝试删除这样的元素
//HTML MARKUP
<div id="div1">
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
//Javascript
var list=document.getElementById('div1');
list.removeChild(list.getElementById('div2'));