<!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>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#inputText {
width: 500px;
height: 40px;
}
#publish {
width: 40px;
height: 30px;
margin-left: 300px;
background-color: #fff;
border: 1px solid #666;
outline: none;
color: #999;
}
.pub {
margin-top: 30px;
}
#box {
width: 500px;
margin-top: 50px;
position: relative;
}
#box button:nth-child(2) {
width: 40px;
height: 20px;
position: absolute;
margin-left: 400px;
top: 10px;
}
#box div {
position: relative;
margin: 20px 0px;
background-color: #999;
height: 40px;
line-height: 40px;
}
#box button:nth-child(3) {
position: absolute;
width: 40px;
height: 20px;
top: 10px;
margin-left: 450px;
}
#box span {
position: absolute;
}
#amendText input {
width: 400px;
height: 80px;
border: 1px solid #ccc;
outline: none;
}
#amendText button {
width: 40px;
height: 20px;
}
#amendText {
position: absolute;
top: 100px;
left: 300px;
}
#amendText button {
position: absolute;
top: 63px;
left: 405px;
}
</style>
</head>
<body>
<h2>网友评论</h2>
<p>
<input type="text" id="inputText" placeholder="我有话想说...">
</p>
<p class="pub">
还可以输入<span id="num">20</span>个字
<input type="button" value="发布" id="publish">
</p>
<div id="box"></div>
<div id="amendText"></div>
<script>
var inputText = document.getElementById('inputText');
var num = document.getElementById('num');
var box = document.getElementById('box');
var amendText = document.getElementById('amendText');
inputText.oninput = function () {
if (inputText.value.length > 20) {
inputText.value = inputText.value.slice(0, 20);
}
num.innerText = 20 - inputText.value.length;
publish.style.color = 'black';
}
publish.onclick = function () {
var newDiv = document.createElement('div');
var compile = document.createElement('button');
var amend = document.createElement('button');
var newSpan = document.createElement('span');
compile.innerText = '编辑';
amend.innerText = '删除';
newSpan.innerText = inputText.value;
console.log(newSpan);
newDiv.appendChild(newSpan);
newDiv.appendChild(compile);
newDiv.appendChild(amend);
if(box.children.length==0){
box.appendChild(newDiv);
}else{
box.insertBefore(newDiv,box.firstElementChild);
}
inputText.value = ''
num.innerText = 20;
publish.style.color = '#999';
box.style.border = '2px solid #ccc'
compile.onclick = function () {
var pNew = document.createElement('input');
var butt = document.createElement('button');
butt.innerText = '修改';
pNew.value = newSpan.innerText;
amendText.appendChild(pNew);
amendText.appendChild(butt);
butt.onclick = function () {
newSpan.innerText = pNew.value;
amendText.style.display = 'none';
amendText.innerHTML = '';
}
pNew.oninput = function () {
if (pNew.value.length > 20) {
pNew.value = pNew.value.slice(0, 20);
}
}
amendText.style.display = 'block';
}
amend.onclick = function () {
box.removeChild(this.parentNode);
if(box.children.length==0){
box.style.display='none';
}
}
box.style.display='block';
}
</script>
</body>
</html>