直接上源代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
body {
font-family: '微软雅黑';
font-size: 14px;
}
.box {
width: 700px;
height: 500px;
border: green 1px solid;
margin: 50px auto 0;
position: relative;
}
.passBtn {
width: 140px;
height: 50px;
background-color: rgb(7, 153, 14);
border: none;
color: #fff;
font-weight: bold;
font-size: 16px;
border-radius: 3px;
margin: 5px 10px;
}
li>img {
width: 60px;
border-radius: 50%;
float: left;
margin-right: 10px;
}
li {
border-bottom: 1px dashed #ccc;
padding: 10px 0;
height: 60px;
margin: 0 30px;
position: relative;
}
li h2 {
font-size: 16px;
/* margin-top: 5px; */
font-weight: bold;
}
li h3 {
font-size: 15px;
margin-top: 5px;
font-weight: normal;
display: inline-block;
margin-right: 50px;
}
li h4 {
font-size: 13px;
margin-top: 5px;
font-weight: normal;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 350px;
}
li p {
display: inline-block;
font-size: 11px;
position: absolute;
right: 0;
bottom: 10px;
color: #ccc;
}
.postBox {
width: 600px;
height: 400px;
border: none;
box-shadow: 2px 2px 4px 2px #888888;
position: absolute;
left: 50px;
top: 50px;
padding: 10px;
background-color: #fff;
display: none;
}
.postBox .txt {
width: 500px;
height: 40px;
border: 1px solid #ccc;
padding-left: 20px;
margin: 0 auto;
}
.postBox .ssbk {
margin: 20px 20px;
}
.ssbk select {
width: 200px;
height: 30px;
}
.post-content {
margin-top: 20px;
}
.post-content textarea {
width: 500px;
resize: none;
}
.postBox .postBtn {
width: 140px;
height: 40px;
background-color: rgb(7, 153, 14);
color: #fff;
border: none;
margin-top: 15px;
border-radius: 3px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="box">
<button class="passBtn">我要发贴</button>
<hr>
<ul class="list">
</ul>
<div class="postBox">
<input type="text" placeholder="请输入标题" class="txt">
<div class="ssbk">
所属版块:
<select name="" id="selPlate">
<option>请选择版块</option>
<option>电子书籍</option>
<option>新课来了</option>
<option>新手报到</option>
</select>
<div class="post-content">
<textarea name="" id="" cols="30" rows="10"></textarea>
</div>
<button class="postBtn">发布</button>
</div>
</div>
</div>
<script>
let passBtn = document.querySelector('.passBtn')
let postBox = document.querySelector('.postBox')
let postBtn = document.querySelector('.postBtn')
let list = document.querySelector('.list');
let arrImg = ['./photo/images/tou01.jpg', './photo/images/tou02.jpg', './photo/images/tou03.jpg',
'./photo/images/tou04.jpg'
]
passBtn.onclick = function () {
postBox.style.display = "block"
}
postBtn.onclick = function () {
let newli = document.createElement('li');
let newimg = document.createElement('img');
let randomNum = Math.floor(Math.random() * 4)
newimg.src = arrImg[randomNum];
let myh2 = document.createElement('h2')
let txt = document.querySelector('.txt')
myh2.innerHTML = txt.value
txt.value = ""
let myh3 = document.createElement('h3');
let myspan = document.createElement('span');
myspan.className = 'plate'
let selPlate = document.querySelector('#selPlate');
myspan.innerHTML = "版块:" + selPlate.value;
myh3.appendChild(myspan)
let myp = document.createElement('p')
let myspanTime = document.createElement('span');
myspanTime.className = 'postTime'
let now = new Date();
let mytime = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate()
// myspanTime.innerHTML = "发表时间:" + now.toLocaleDateString();
myspanTime.innerHTML = "发表时间:" + mytime;
myp.appendChild(myspanTime)
let summary = document.createElement('h4')
let textArea = document.querySelector('textarea')
summary.innerHTML = textArea.value
newli.appendChild(newimg);
newli.appendChild(myh2);
newli.appendChild(myh3);
newli.appendChild(summary);
newli.appendChild(myp);
console.log(list.firstChild);
if (list.firstChild) {
list.insertBefore(newli, list.firstChild)
} else {
list.appendChild(newli)
}
postBox.style.display = 'none'
}
</script>
</body>
</html>



结束,谢谢观看!