<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>信息登记表</title>
<style>
body {
height: 670px;
width: 400px;
margin: auto;
border: 1px solid orange;
}
h2 {
text-align: center;
color: green;
}
#div1 {
width: 400px;
height: 100px;
background-color: gray;
text-align: center;
}
#div2 {
width: 400px;
height: 500px;
background-color: pink;
}
#input {
width: 300px;
height: 30px;
font-size: 20px;
text-align: center;
}
#div1 button {
width: 100px;
height: 30px;
font-size: 18px;
background-color: violet;
box-shadow: 10px 10px 5px 0px blue red;
}
#div2 div {
border-bottom: 1px solid black;
padding: 2px;
position: relative;
}
#div2 div button {
position: absolute;
right: 0px;
text-align: center;
}
</style>
<script>
function randomColor() {
var str = "rgba(" + parseInt(Math.random() * 256) + "," + parseInt(Math.random() * 256) + "," +
parseInt(Math.random() * 256) + ",1)";
return str;
}
</script>
<script>
window.onload = function() {
var oDiv1 = document.getElementById("div1");
var oDiv2 = document.getElementById("div2");
var oInput = document.getElementById("input");
var aBtns = oDiv1.getElementsByTagName("button");
aBtns[0].onclick = function() {
if (!oInput.value) {
alert("输入内容不能为空");
} else {
var newDiv = document.createElement("div");
var oTet = document.createTextNode(oInput.value);
newDiv.appendChild(oTet);
newDiv.style.backgroundColor = randomColor();
oDiv2.appendChild(newDiv);
oInput.value = '';
}
}
aBtns[1].onclick = function() {
oDiv2.removeChild(oDiv2.lastChild);
}
aBtns[2].onclick = function() {
var newNode = oDiv2.lastChild.cloneNode(true);
oDiv2.appendChild(newNode);
}
}
</script>
</head>
<body>
<h2>成员信息登记表</h2>
<div id="div1">
<input type="text" id="input" placeholder="输入内容">
<br>
<button>增加</button>
<button>删除</button>
<button>复制</button>
</div>
<div id="div2">
<!-- <div>.. <button>!</button></div>
<div>.. <button>!</button></div>
<div>.. <button>!</button></div> -->
</div>
</body>
</html>
插入,删除,复杂操作,可以直接使用
最新推荐文章于 2024-08-31 21:49:18 发布