博客列表 >留言小作业

留言小作业

不露声色
不露声色原创
2021年04月21日 10:10:23733浏览

输入留言并在下方进行展示,可以删除当前评论,评论的字数不能超过100,如果超过则仍显示当前展示内容

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>留言板 / 评论</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. li {
  15. list-style: none;
  16. }
  17. body {
  18. background-color: rgb(174, 236, 236);
  19. color: #555;
  20. }
  21. .comment {
  22. width: 85%;
  23. margin: 1em auto;
  24. display: grid;
  25. gap: 0.5em;
  26. }
  27. .comment #content {
  28. resize: none;
  29. border: none;
  30. padding: 0.5em;
  31. outline: none;
  32. }
  33. .comment #content:focus,
  34. .comment #content:hover {
  35. box-shadow: 0 0 8px steelblue;
  36. transition: 0.6s;
  37. }
  38. .comment .submit {
  39. width: 30%;
  40. margin-left: auto;
  41. background-color: lightseagreen;
  42. border: none;
  43. outline: none;
  44. color: white;
  45. height: 2.5em;
  46. }
  47. .comment .submit:hover {
  48. background-color: seagreen;
  49. transition: 0.6s;
  50. cursor: pointer;
  51. }
  52. .list {
  53. width: 80%;
  54. /* background-color: yellow; */
  55. margin: auto;
  56. padding: 1em;
  57. }
  58. .list li {
  59. border-bottom: 1px solid #fff;
  60. padding-bottom: 20px;
  61. margin-bottom: 20px;
  62. }
  63. .del-btn {
  64. background-color: wheat;
  65. color: #000;
  66. padding: 0.5em 1em;
  67. /* height: 2.2em; */
  68. border: none;
  69. outline: none;
  70. }
  71. .del-btn:hover {
  72. cursor: pointer;
  73. background-color: rgb(31, 230, 163);
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <form action="" class="comment" id="comment">
  79. <label for="content">请留言</label>
  80. <textarea name="content" id="content" cols="30" rows="10"></textarea>
  81. <span class="notice">还可以输入100字</span>
  82. <button class="submit" type="button" name="submit">提交</button>
  83. </form>
  84. <ul class="list"></ul>
  85. </body>
  86. </html>

js 部分

  1. <script>
  2. //获取元素
  3. // form
  4. let comment = document.forms.comment;
  5. console.log(comment);
  6. let content = comment.content;
  7. let btn = comment.submit;
  8. let commentList = document.querySelector(".list");
  9. let notice = document.querySelector(".notice");
  10. // 2给按钮添加事件
  11. btn.onclick = (ev) => {
  12. //获取留言内容
  13. //console.log(content.value.trim().length);
  14. // 长度不能超过100 而且大于0
  15. if (content.value.trim().length > 0 && content.value.trim().length <= 100) {
  16. // 创建留言内容标签然后插入到ul中
  17. let li = document.createElement("li");
  18. li.textContent = content.value;
  19. // 添加删除按钮
  20. let delbtn = document.createElement("div");
  21. delbtn.textContent = "删除";
  22. delbtn.style.float = "right";
  23. delbtn.classList.add("del-btn");
  24. delbtn.onclick = function () {
  25. if (confirm('是否删除')) {
  26. // li.remove();
  27. this.parentNode.remove();
  28. alert("留言删除成功");
  29. }
  30. }
  31. li.append(delbtn);
  32. commentList.prepend(li);
  33. } else {
  34. alert("长度超过100");
  35. //超出之后并将输入内容清空
  36. // content.value = '';
  37. content.focus();
  38. return false;
  39. }
  40. }
  41. content.oninput = function () {
  42. let textLength = content.value.trim().length;
  43. notice.textContent = `还可以输入${100 - textLength}个字`;
  44. let lastword = 100 - textLength;
  45. if (lastword <= 0) {
  46. alert("已输满100字");
  47. content.value = content.value.trim().substring(0, 100);
  48. // content.disabled = true;
  49. // console.log(content.disabled);
  50. notice.textContent = `还可以输入0个字`;
  51. content.focus();
  52. } else {
  53. }
  54. }
  55. </script>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议