登录  /  注册
博主信息
博文 98
粉丝 1
评论 0
访问量 77999
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
留言板学习
阿杰
原创
677人浏览过

留言板

  • css部分
  1. *{
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. }
  6. .messCont{
  7. width: 300px;
  8. min-height: 300px;
  9. background-color: bisque;
  10. margin: 10vh auto;
  11. border-radius: 10px;
  12. padding: 10px;
  13. }
  14. .title,.inputCont{
  15. text-align: center;
  16. line-height: 3vh;
  17. }
  18. .list{
  19. margin-top: 2vh;
  20. }
  21. .set{
  22. margin: 10px 0 10px 20px;
  23. }
  • html部分
  1. <div class="messCont">
  2. <div class="title">留言板</div>
  3. <div class="inputCont">
  4. <input type="text" onkeydown="addMsg(this)" placeholder="请输入内容" autofocus />
  5. </div>
  6. <ul class="list"></ul>
  7. </div>
  • js部分
  1. function addMsg(ele) {
  2. // console.log(event);
  3. // console.log(event.key);
  4. if (event.key === 'Enter') {
  5. // 1. 获取显示留言的容器
  6. const ul = document.querySelector('.list');
  7. // 2. 判断用户留言是否为空
  8. if (ele.value.trim().length === 0) {
  9. alert('留言不能为空');
  10. ele.focus();
  11. return false;
  12. }
  13. // 3. 添加一条新留言
  14. const li = document.createElement('li');
  15. // console.log(ele.value);
  16. // li.textContent = ele.value;
  17. li.innerHTML = '<div>' + '留言:'+ ele.value + '<button onclick="del(this.parentNode)">删除</button>'+'</div>';
  18. li.className = 'item';
  19. // if (ul.firstElementChild !== null) {
  20. // ul.firstElementChild.before(li);
  21. // } else {
  22. // ul.append(li);
  23. // }
  24. ul.insertAdjacentElement('afterBegin', li);
  25. // 4. 将输入框中的前一条留言清空
  26. ele.value = null;
  27. // 5. 焦点重置
  28. ele.focus();
  29. // 回复
  30. setTimeout(function(){
  31. const replay = document.createElement('div');
  32. replay.className = 'set';
  33. replay.textContent = '感谢留言!';
  34. let lastEle = document.querySelector('.list>.item:first-of-type>div');
  35. lastEle.insertAdjacentElement('afterEnd', replay);
  36. },1500);
  37. }
  38. }
  39. // 删除
  40. function del(ele) {
  41. return confirm('是否删除?') ? ele.remove() : false;
  42. }
  • 效果

批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学