博客列表 >实战评论区及自动回复

实战评论区及自动回复

子墨吖ฅฅ
子墨吖ฅฅ原创
2022年11月09日 14:25:23329浏览

JS部分

  1. const insertComment = function (ele,isBtn = false) {
  2. if (event.key === 'Enter' || isBtn) {
  3. // 1. 非空判断
  4. if (ele.value.length === 0) {
  5. // 提示用户
  6. alert('留言不能为空');
  7. // 重置焦点
  8. ele.focus();
  9. // 直接返回
  10. return false;
  11. }
  12. const ul = document.querySelector('.container_body');
  13. ele.value = `<div class="personal_body"><div class="comment_body">${ele.value}</div>`
  14. ele.value += `<div class="submit_del"><button onclick="del(this.parentNode)">删除评论</button></div>`;
  15. ul.insertAdjacentHTML(`afterend`, `${ele.value}<div class="zdhf">自动回复内容</div><br><hr>`);
  16. // 3. 清空输入框
  17. ele.value = null;
  18. }
  19. };
  20. submit_btn.onclick = (e)=>{
  21. const text = document.getElementById("pl")
  22. insertComment(text,true);
  23. }
  24. // 删除
  25. const del = function (ele) {
  26. console.log(ele)
  27. return confirm('是否删除?') ? (ele.parentNode.outerHTML = null) : false;
  28. };

CSS部分

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .container{
  6. display: grid;
  7. place-content: center;
  8. }
  9. .main{
  10. display: grid;
  11. }
  12. .comment,.submit,.comment_body{
  13. background-color: lightskyblue;
  14. padding: 5px;
  15. }
  16. .comment{
  17. width: 400px;
  18. height: 200px;
  19. border-radius: 30px 30px 0 0;
  20. display: grid;
  21. }
  22. .comment textarea{
  23. width: 380px;
  24. height:190px;
  25. border: none;
  26. border-radius: 10px;
  27. display: grid;
  28. place-self: center center;
  29. }
  30. .submit{
  31. width: 400px;
  32. border-radius: 0 0 30px 30px;
  33. display: grid;
  34. }
  35. .submit button{
  36. background-color: pink;
  37. border: none;
  38. border-radius: 30px;
  39. padding: 10px;
  40. display: grid;
  41. place-self: center center;
  42. }
  43. input{
  44. border: none;
  45. border-radius: 10px;
  46. }
  47. /*接收评论区部分*/
  48. .container_body{
  49. width: 400px;
  50. margin-top: 30px;
  51. display: grid;
  52. place-content: center;
  53. }
  54. .personal_body{
  55. display: grid;
  56. place-self: center center;
  57. }
  58. .comment_body{
  59. width: 400px;
  60. min-height: 50px;
  61. border-radius: 30px;
  62. display: grid;
  63. place-self: center center;
  64. text-align: center;
  65. }
  66. .submit_del{
  67. width: 400px;
  68. border-radius: 0 0 30px 30px;
  69. display: grid;
  70. place-self: center center;
  71. text-align: center;
  72. }
  73. .submit_del button{
  74. border: none;
  75. background-color: pink;
  76. border-radius: 30px;
  77. padding: 10px;
  78. }
  79. .zdhf{
  80. width: 400px;
  81. min-height: 50px;
  82. background-color: lightgreen;
  83. border-radius: 30px;
  84. margin-top: 5px;
  85. display: grid;
  86. place-self: center center;
  87. text-align: center;
  88. }

HTML部分

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>实战评论功能</title>
  9. </head>
  10. <link rel="stylesheet" href="work.css">
  11. <body>
  12. <section class="container">
  13. <h2>评论区</h2>
  14. <div class="main">
  15. <div class="comment">
  16. <textarea name="pl" id="pl" cols="30" rows="10" placeholder="请输入你的评论内容" onkeydown="insertComment(this)"></textarea>
  17. </div>
  18. <div class="submit">
  19. <button type="submit" id="submit_btn">提交评论/或按回车提交</button>
  20. </div>
  21. </div>
  22. </section>
  23. <section class="container_body">
  24. </section>
  25. </body>
  26. <script src="work.js"></script>
  27. </html>

效果图

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议