javascript 如何使用javascript删除div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7307776/
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 do I remove a div using javascript?
提问by Bas Slagter
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
//function del () {
var x= new Date ();
var date= x.getDate ();
var hours= x.getHours();
var minutes=x.getMinutes();
var sec= x.getSeconds();
document.write(minutes +":"+ sec )
if (minutes=="33"){
//alert ("time is over")
document.getElementById('airtel').removeChild(airtel);
}
//}
</script>
</head>
<body>
<div id="airtel">
<div id="vodafone">vodaphone</div>
<div id="idea">idea</div>
</div>
</body>
</html>
回答by svrx
Using jQueryLibrary removes the pain from using Javascript for handling the DOM, i sugest you give it a try.
使用jQuery库消除了使用 Javascript 处理 DOM 的痛苦,我建议您尝试一下。
But if you just want that working in native Javascript, what you need to do is traverse to the parentNode and then remove the child you want.
但是如果你只想在原生 Javascript 中工作,你需要做的是遍历 parentNode,然后删除你想要的孩子。
var element = document.getElementById('airtel');
element.parentNode.removeChild(element);
回答by Bas Slagter
回答by medopal
You should get the element of 'airtel' then remove it from it's parent, which is the Body tag.
您应该获取 'airtel' 元素,然后将其从它的父元素(即 Body 标记)中删除。
var airtel= document.getElementById('airtel');
document.getElementsByTagName('body')[0].removeChild(airtel);