博主信息
博文 34
粉丝 0
评论 0
访问量 32453
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
javascript-基础(四)实战
CY明月归
原创
965人浏览过

1. 独立完成留言板功能,要求使用CSS进行页面美化

html部分
  1. <body style="background-color: rgb(197, 234, 255)">
  2. <p
  3. style="text-align: center; font-size: x-large; color: rgb(5, 119, 148)"
  4. class="iconfont icon-liuyan"
  5. >
  6. 留言板
  7. </p>
  8. <hr />
  9. <div
  10. style="
  11. display: grid;
  12. grid-template-columns: 15vw 25vw 25vw repeat(2, 1fr);
  13. place-items: center;
  14. "
  15. >
  16. <div></div>
  17. <input
  18. type="text"
  19. onkeydown="addMsg()"
  20. placeholder="回车或者点击提交留言"
  21. style="
  22. border: none;
  23. border-radius: 10px;
  24. width: 200px;
  25. height: 22px;
  26. padding: 3px;
  27. "
  28. autofocus
  29. />
  30. <span
  31. onclick="msg()"
  32. style="font-size: large; color: rgb(3, 151, 23)"
  33. class="iconfont icon-kongxinduigou"
  34. >提交</span
  35. >
  36. <span
  37. onclick="delStep()"
  38. style="font-size: larger; color: rgb(206, 154, 13)"
  39. class="iconfont icon-shanchu"
  40. >逐条
  41. </span>
  42. <span
  43. onclick="delAll()"
  44. style="font-size: larger; color: rgb(230, 9, 9)"
  45. class="iconfont icon-shanchu"
  46. >所有
  47. </span>
  48. </div>
  49. <ul
  50. class="box"
  51. style="font-size: large; font-weight: bold; margin: 20px 0 0 50px"
  52. ></ul>
  53. </body>
js重要部分
  1. function addMsg() {
  2. console.log(event.key);
  3. //1、获取容器√
  4. //2、向容器添加留言内容√
  5. //3、清空输入框√
  6. //4、设置焦点√
  7. ul = document.querySelector(".box");
  8. if (event.key == "Enter") {
  9. inputvalue = document.querySelector("input").value.trim();
  10. if (inputvalue.length == 0) {
  11. alert("内容为空,请检查");
  12. } else {
  13. let li = document.createElement("li");
  14. //li.textContent = inputvalue;
  15. li.innerHTML = inputvalue + "<span onclick='del(this.parentNode)' style='font-size: larger; color: rgb(116,154, 123)' class='iconfont icon-shanchu'></span>";
  16. ul.insertAdjacentElement("afterbegin", li);
  17. }
  18. document.querySelector("input").value = null;
  19. }
  20. }

2. (选做)将留言板功能升级为自动客服回复(定时器实现)

  1. function sendMsg() {
  2. //1、获取容器√
  3. //2、向容器添加留言内容√
  4. //3、清空输入框√
  5. //4、设置焦点√
  6. content = document.querySelector(".msg-content");
  7. inputvalue = document.querySelector("textarea").value.trim();
  8. if (inputvalue.length == 0) {
  9. alert("内容为空,请检查");
  10. } else {
  11. let msg = document.createElement("li");
  12. msg.innerHTML =
  13. '<span class="saycontent">' +
  14. inputvalue +
  15. "</span>" +
  16. '<span class="iconfont icon-denglu1 dl"></span>';
  17. // msg.innerHTML = inputvalue + '<img src="imgs/cat.jpg" alt="">'
  18. content.insertAdjacentElement("beforeEnd", msg);
  19. setInterval("robotSay()",5000);
  20. }
  21. document.querySelector("textarea").value = "";
  22. }
  23. function robotSay() {
  24. let li = document.createElement("li");
  25. li.className = 'rgkf'
  26. saythings = ['你好啊!', '正在转人工客服...', '请不要着急...', '吃晚饭了吗?', '我在PHP中文网学习','再见','beybey!']
  27. console.log(Math.floor(Math.random()*7))
  28. let say = saythings[Math.floor(Math.random()*7)]
  29. li.innerHTML =
  30. '<div class="msg-content-robot"><img src="imgs/cat.jpg" alt=""><span>' + say + '</span></div>';
  31. content.insertAdjacentElement("beforeEnd", li);
  32. }
批改老师:PHPzPHPz

批改状态:合格

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

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

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