Javascript添加div到前面两个div中间后删除div,请问下这个怎么改,只会添加不会删除也不会添到指定位置
<head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title>无标题文档</...
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.div1{width:50px;height:50px;background-color:red}
.div2{width:50px;height:50px;background-color:green}
</style>
<div class="div1"></div>
<div class="div2"></div>
<input type="button" onclick="aa()" value="添加" />
<input type="button" onclick="sc()" value="删除" />
</head>
<body>
<script>
function aa() {
var div = document.createElement("div");
div.style.backgroundColor="blue";
div.style.height="50px";
div.style.width="50px";
document.body.appendChild(div);
}
function sc(){
}
</script>
</body> 展开
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.div1{width:50px;height:50px;background-color:red}
.div2{width:50px;height:50px;background-color:green}
</style>
<div class="div1"></div>
<div class="div2"></div>
<input type="button" onclick="aa()" value="添加" />
<input type="button" onclick="sc()" value="删除" />
</head>
<body>
<script>
function aa() {
var div = document.createElement("div");
div.style.backgroundColor="blue";
div.style.height="50px";
div.style.width="50px";
document.body.appendChild(div);
}
function sc(){
}
</script>
</body> 展开
1个回答
展开全部
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.div1{width:50px;height:50px;background-color:red}
.div2{width:50px;height:50px;background-color:green}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<input type="button" onclick="aa()" value="添加" />
<input type="button" onclick="sc()" value="删除" />
<script>
function aa() {
var div = document.createElement("div");
div.id="newDiv";
div.style.backgroundColor="blue";
div.style.height="50px";
div.style.width="50px";
document.body.insertBefore(div,document.getElementsByClassName("div2")[0]);
}
function sc(){
document.body.removeChild(document.getElementById("newDiv"));
}
</script>
</body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.div1{width:50px;height:50px;background-color:red}
.div2{width:50px;height:50px;background-color:green}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<input type="button" onclick="aa()" value="添加" />
<input type="button" onclick="sc()" value="删除" />
<script>
function aa() {
var div = document.createElement("div");
div.id="newDiv";
div.style.backgroundColor="blue";
div.style.height="50px";
div.style.width="50px";
document.body.insertBefore(div,document.getElementsByClassName("div2")[0]);
}
function sc(){
document.body.removeChild(document.getElementById("newDiv"));
}
</script>
</body>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询