html,点击每个圆圈加号就添加一个格子,格子可以输入文字,点击圆圈减号就删掉对应的格子
时间: 2024-05-01 17:16:37 浏览: 74
以下是一个简单的HTML代码示例,点击加号会在下方添加一个文本框格子,点击减号会删除对应的格子:
```html
<!DOCTYPE html>
<html>
<head>
<title>添加/删除格子</title>
<style>
#container {
display: flex;
flex-wrap: wrap;
}
.box {
width: 150px;
height: 150px;
border: 1px solid black;
margin: 10px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 24px;
text-align: center;
}
.plus, .minus {
cursor: pointer;
font-size: 30px;
margin: 5px;
user-select: none;
}
.plus:hover, .minus:hover {
color: blue;
}
</style>
</head>
<body>
<div id="container"></div>
<button id="add-box">添加格子</button>
<script>
var container = document.getElementById("container");
var addButton = document.getElementById("add-box");
addButton.addEventListener("click", function() {
var newBox = document.createElement("div");
newBox.classList.add("box");
var textInput = document.createElement("input");
textInput.type = "text";
textInput.placeholder = "输入文字";
newBox.appendChild(textInput);
var plusButton = document.createElement("span");
plusButton.classList.add("plus");
plusButton.textContent = "+";
newBox.appendChild(plusButton);
var minusButton = document.createElement("span");
minusButton.classList.add("minus");
minusButton.textContent = "-";
newBox.appendChild(minusButton);
container.appendChild(newBox);
plusButton.addEventListener("click", function() {
var newTextInput = document.createElement("input");
newTextInput.type = "text";
newTextInput.placeholder = "输入文字";
newBox.insertBefore(newTextInput, minusButton);
});
minusButton.addEventListener("click", function() {
container.removeChild(newBox);
});
});
</script>
</body>
</html>
```
阅读全文