使用 Javascript 动态创建和删除 Div 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13320691/
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
Dynamically Create and Delete Div tag using Javascript
提问by Nayeem Junaid
I want to create 3 Div tags Dynamically when I press 3 different buttons (Red Green Blue).. When I press Red I want the height of 'red' div to be the size of my screen so that whole page appears to be red and when I press Green I want the height of the 'red' div to become 50% of my screen and other 50% of the screen should be occupied by other div (which is Green)..
当我按下 3 个不同的按钮(红绿蓝)时,我想动态地创建 3 个 Div 标签。当我按下红色时,我希望“红色”div 的高度与我的屏幕一样大,这样整个页面看起来是红色的,并且当我按下绿色时,我希望“红色”div 的高度成为我屏幕的 50%,而其他 50% 的屏幕应该被其他 div(即绿色)占据。
And finally when I press Blue button I want All the divs to appear on screen with equal height...
最后,当我按下蓝色按钮时,我希望所有的 div 都以相同的高度出现在屏幕上......
I'm able to create the divs but I can't delete the previously created div when I press Red button after pressing all three buttons...
我可以创建 div,但是在按下所有三个按钮后按红色按钮时,我无法删除以前创建的 div...
Here is the code:
这是代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ass 2</title>
<script type="text/javascript">
function fblue() {
// document.bgColor = 'lightblue';
selc=document.getElementById("first");
divBlue=document.getElementById("one");
myPara.removeChild(divBlue);
selc=document.getElementById("first");
divBlue=document.createElement("div");
divBlue.id = "one";
divBlue.style.backgroundColor = "lightblue";
divBlue.style.height = "610px"
divBlue.innerHTML = "dv tag created successfully";
selc.appendChild(divBlue);
}
function fgreen() {
selc=document.getElementById("first");
divBlue=document.getElementById("one");
divBlue.id = "one";
divBlue.style.backgroundColor = "lightblue";
divBlue.style.height = "305px"
divBlue.innerHTML = "dv tag created successfully";
selc.appendChild(divBlue);
divGreen=document.createElement("div");
divGreen.id = "one";
divGreen.style.backgroundColor = "lightgreen";
divGreen.style.height = "305px"
divGreen.innerHTML = "dv tag created successfully";
selc.appendChild(divGreen);
//document.bgColor = '';
}
function fred() {
document.getElementById("one").style.backgroundColor = 'lightblue';
document.getElementById("one").style.width = '1104px';
document.getElementById("one").style.height = '185px';
document.getElementById("two").style.backgroundColor = 'red';
document.getElementById("two").style.width = '1104px';
document.getElementById("two").style.height = '185px';
document.getElementById("three").style.backgroundColor = 'lightgreen';
document.getElementById("three").style.width = '1104px';
document.getElementById("three").style.height = '185px';
document.bgColor = '';
}
</script>
</head>
<body style="margin: 0px;">
<div style="height: 0px;" id="first"></div>
<div id="four" style="margin-left: 415px; margin-top: 500px">
<form>
<input type="button" name="blu" value="Blue" onclick="javascript:fblue();">
<input type="button" name="gre" value="Green/Red" onclick="javascript:fgreen();">
<input type="button" name="re" value="Red/Green/Blue" onclick="javascript:fred();">
</form>
</div>
</body>
</html>
回答by defau1t
document.createElement("div");
will create a div dynamiclly
将动态创建一个 div
var oldChild = element.removeChild(child);
element.removeChild(child);
Removes a child node from the DOM. Returns removed node.
从 DOM 中移除一个子节点。返回删除的节点。