推荐在写动态生成标签数据的时候,提前写一遍html+css的结构,方便提供写照模板
<div class="box"><div class="inputBox"><textarea maxlength="200"></textarea></div><div class="btn"><div class="btns"><span class="num1">0</span><span class="num2">/200</span><input type="button" value="发布"></div></div><div class="content"></div></div>
<style>* {margin: 0;padding: 0;border: 0 none;}.box {width: 980px;margin: 50px auto;}.inputBox {width: 100%;height: 80px;}.inputBox textarea {border: 1px solid #ccc;outline-style: none;width: 100%;height: 100%;box-sizing: border-box;resize: none;border-radius: 10px;padding: 20px;}.btn {overflow: hidden;}.btns {float: right;margin: 10px 0;height: 40px;}.btns input {width: 240px;height: 40px;color: #fff;background-color: skyblue;outline-style: none;cursor: pointer;}.content {width: 100%;}.item {width: 100%;border-bottom: 1px solid #ccc;padding-top: 10px;padding-bottom: 10px;overflow: hidden;}.leftIMg {float: left;width: 100px;}.leftIMg img {width: 100%;}.rightImg {float: left;margin-left: 10px;line-height: 30px;}</style>
<script type="text/javascript">var input = document.querySelector('.inputBox textarea');var num = document.querySelector('.num1');input.oninput = function() {num.innerHTML = this.value.length;}var btn = document.querySelector('.btns input');btn.onclick = function() {var div_1 = document.createElement('div');div_1.className = 'item';var div2 = document.createElement('div');div2.className = 'leftIMg';div2.innerHTML = '<img src='+getImg()+'>';var div3 = document.createElement('div');div3.className = 'rightImg';div3.innerHTML = '<p>'+getName()+'</p><p>'+getTime()+'</p><p>'+input.value+'</p>';div_1.appendChild(div2);div_1.appendChild(div3);var content = document.querySelector('.content');content.insertBefore(div_1, content.firstElementChild);}var names = ['阿甘', '特朗普', '萌妹子', '屌丝男士', '极品女士', '二狗子', '鸡腿子', '肉肉'];var imgs = ['img/01.jpg', 'img/02.jpg', 'img/04.jpg', 'img/06.jpg', 'img/09.jpg', 'img/14.jpg', 'img/17.jpg'];function getRandom(n, m) {return Math.floor(Math.random() * (m - n + 1) + n);}function getImg() {return imgs[getRandom(0, imgs.length-1)]; }function getName () {return names[getRandom(0, names.length-1)];}function getTime() {var d = new Date();var y = d.getFullYear();var m = d.getMonth() + 1;m = m >= 10 ? m : '0' +m;var d1 = d.getDate();d1 = d1 >= 10? d1 : '0' + d1;var h = d.getHours();h = h >= 10 ? h : '0' + h;var m1 = d.getMinutes();m1 = m1 >= 10? m1 : '0' + m1;var s1 = d.getSeconds();s1 = s1 >= 10? s1 : '0' + s1;return y + '-' + m + '-' + d1 + ' ' + h + ':' + m1 + ':' + s1;}</script>
实现的效果: