实现目标
简单的评论效果,点击发布即可发布最新评论
代码实现
<!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;
}
</style>
</head>
<body>
<textarea name="" id="" cols="30" rows="10"></textarea>
<button>发布</button>
<ul>
<li>123</li>
</ul>
<script>
var btn = document.querySelector('button');
var text = document.querySelector('textarea');
var ul = document.querySelector('ul');
// 2.注册事件
btn.onclick = function () {
if (text.value == '') {
alert('您还没有输入内容');
return false;
} else {
//创建元素
var li = document.createElement('li');
li.innerHTML = text.value;
ul.insertBefore(li, ul.children[0]);
text.value = '';
}
}
</script>
</body>
</html>
直接复制粘贴即可运行
仅供功能学习,未进行css美化
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容