简体   繁体   English

如何使用javascript删除子div

[英]how to remove child div using javascript

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. 当我点击按钮我需要删除子div(div2)如何做到这一点。 using this code i am facing problem please tell. 使用此代码我面临问题请告诉。 help me . 帮我 。 we have any other solution for this problem 我们有任何其他解决方案来解决这个问题

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);

You need to find div2 parent and then you can use removeChild to remve div2 你需要找到div2 parent,然后你可以使用removeChild来提取div2

var list=document.getElementById("div2");
var parentDiv = list.parentNode;
parentDiv.removeChild(list);

Demo 演示

Problem in your code 您的代码中存在问题

list.removeChild("div2"")); <<== ") is additional

Use modern JS! 使用现代JS!

const div = document.getElementById("div2");
div.remove();

or just 要不就

document.getElementById("div2").remove()

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'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM