一.知识点回顾
①事件委托
父元素.onclick = function(e){
e = window.event || e.event; //浏览器兼容性
console(e.target)
}
②字符串方法
trim() //删除前置以及后缀的所有空格,然后返回结果;
有兴趣的话前面文章有封装方法可参考
var str = " hello world ";
var newStr = str.trim();
console.log(newStr); //"hello world"
二.代码详情
- html详情
<div>
<ul id="content"></ul>
<textarea name="" id="text" cols="30" rows="10"></textarea>
<button id="go">发表</button>
</div>
2.css简写
* {
list-style: none;
}
.active {
display: none;
}
3.js代码详情
var ul = document.getElementById('content')
var text = document.getElementById('text')
var go = document.getElementById('go')
var content = document.getElementById('content')
function getHtml() {
var time = new Date();
var timer = `${time.getFullYear()}-${time.getMonth() +1}-${time.getDate()}
${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`
var text_val = text.value.trim();
if (text_val == '' && text_val.length == 0) {
return;
}
console.log(text_val, text_val.length)
ul.innerHTML += `<li><span>${timer}</span>${text_val}<button>删除</button></li>`;
text.value = '';
}
go.addEventListener('click', function () {
getHtml()
})
content.onclick = function (e) {
e = window.event || e.event;
console.log(e.target)
if (e.target.nodeName == 'BUTTON') {
e.target.parentNode.classList.add('active')
}
}
window.onkeydown = function (e) {
e = window.event || e.event;
if (e.keyCode == 13) {
getHtml()
}
}
如有错误请指正,抱拳了老铁!!!