<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>删除自己的div</title>
<style>
.wrap {
width: 400px
height: 400px
background-color: aqua
}
.user-info {
width: 390px
height: 120px
border: 1px solid red
}
</style>
</head>
<body>
<div class="wrap">
<div class="del">
<div class="user-info">我是1</div>
<div class="user-info">我是2</div>
<div class="user-info">我是3</div>
</div>
</div>
<script>
var del = document.getElementsByClassName("del")[0]
var user = document.getElementsByClassName("user-info")
for (let i = 0i <user.lengthi++) {
user[i].onclick = function () {
this.parentNode.removeChild(this)
}
}
</script>
</body>
</html>
代码如下:function addDiv(w,h){
deleteDiv()
//创建一个div
var my = document.createElement("divCell")
//添加到页面
document.body.appendChild(my)
my.style.position="absolute"
//通过样式指定x坐标(随机数0~450)
my.style.top= Math.round(Math.random()*450)
//通过样式指定y坐标(随机数0~700)
my.style.left= Math.round(Math.random()*700)
//通过样式指定宽度
my.style.width=w
//通过样式指定高度
my.style.height=h
//通过样式指定背景颜色,,若是背景图片 例为my.style.backgroundImage="url(img/3.jpg)"
my.style.backgroundColor="#ffffcc"
//添加div的内容
//my.innerHTML=i++
//设置样式透明
my.style.filter = "alpha(opacity=50)"
//设置ID
my.id = "divCell"
}
function deleteDiv()
{
var my = document.getElementById("divCell")
if (my != null)
my.parentNode.removeChild(my)
}