task-comp.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>评论组件</title>
  5. <style>
  6. /* 样式 */
  7. .comment-container {
  8. max-width: 500px;
  9. margin: 0 auto;
  10. }
  11. .comment {
  12. margin-bottom: 10px;
  13. padding: 10px;
  14. border: 1px solid #ccc;
  15. border-radius: 5px;
  16. position: relative;
  17. }
  18. .comment .author {
  19. font-weight: bold;
  20. display: flex;
  21. align-items: center;
  22. }
  23. .comment .author img {
  24. width: 30px;
  25. height: 30px;
  26. border-radius: 50%;
  27. margin-right: 10px;
  28. }
  29. .comment .actions {
  30. position: absolute;
  31. bottom: 5px;
  32. right: 5px;
  33. display: flex;
  34. align-items: center;
  35. }
  36. .comment .actions button {
  37. padding: 5px 10px;
  38. margin-right: 5px;
  39. }
  40. .comment .like-count,
  41. .comment .reply-count {
  42. font-size: 0.8em;
  43. color: #888;
  44. margin-left: 5px;
  45. }
  46. .comment .timestamp {
  47. font-size: 0.8em;
  48. color: #888;
  49. }
  50. .comment .content {
  51. margin-top: 5px;
  52. }
  53. .comment-form textarea {
  54. width: 100%;
  55. height: 80px;
  56. margin-bottom: 10px;
  57. }
  58. .comment-form button {
  59. padding: 5px 10px;
  60. }
  61. .comment-form.reply-form {
  62. margin-left: 30px;
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <div class="comment-container">
  68. <h2>评论</h2>
  69. <div id="comments"></div>
  70. <form class="comment-form">
  71. <textarea id="comment-input" placeholder="请输入评论内容"></textarea>
  72. <button type="button" onclick="addComment()">发表评论</button>
  73. </form>
  74. </div>
  75. <script>
  76. // JavaScript
  77. function addComment() {
  78. var commentInput = document.getElementById("comment-input");
  79. var commentContent = commentInput.value.trim();
  80. if (commentContent !== "") {
  81. var comment = document.createElement("div");
  82. comment.className = "comment";
  83. var author = document.createElement("div");
  84. author.className = "author";
  85. var avatar = document.createElement("img");
  86. avatar.src = "../img/profile photo.png"; // 替换为实际的头像图片路径
  87. var authorName = document.createElement("span");
  88. authorName.textContent = "匿名用户";
  89. author.appendChild(avatar);
  90. author.appendChild(authorName);
  91. var actions = document.createElement("div");
  92. actions.className = "actions";
  93. var likeButton = document.createElement("button");
  94. likeButton.textContent = "点赞";
  95. likeButton.addEventListener("click", incrementLikeCount);
  96. var likeCount = document.createElement("span");
  97. likeCount.className = "like-count";
  98. likeCount.textContent = "0";
  99. actions.appendChild(likeButton);
  100. actions.appendChild(likeCount);
  101. var replyButton = document.createElement("button");
  102. replyButton.textContent = "回复";
  103. replyButton.addEventListener("click", toggleReplyForm);
  104. var replyCount = document.createElement("span");
  105. replyCount.className = "reply-count";
  106. replyCount.textContent = "0";
  107. actions.appendChild(replyButton);
  108. actions.appendChild(replyCount);
  109. var timestamp = document.createElement("div");
  110. timestamp.className = "timestamp";
  111. timestamp.textContent = new Date().toLocaleString();
  112. var content = document.createElement("div");
  113. content.className = "content";
  114. content.textContent = commentContent;
  115. var replyForm = document.createElement("form");
  116. replyForm.className = "comment-form reply-form hidden";
  117. var replyTextarea = document.createElement("textarea");
  118. replyTextarea.placeholder = "请输入回复内容";
  119. var replyButton = document.createElement("button");
  120. replyButton.type = "button";
  121. replyButton.textContent = "回复";
  122. replyButton.addEventListener("click", addReply);
  123. replyForm.appendChild(replyTextarea);
  124. replyForm.appendChild(replyButton);
  125. comment.appendChild(author);
  126. comment.appendChild(actions);
  127. comment.appendChild(timestamp);
  128. comment.appendChild(content);
  129. comment.appendChild(replyForm);
  130. var commentsContainer = document.getElementById("comments");
  131. commentsContainer.appendChild(comment);
  132. commentInput.value = "";
  133. }
  134. }
  135. function incrementLikeCount(event) {
  136. var likeCountElement = event.target.nextElementSibling;
  137. var currentCount = parseInt(likeCountElement.textContent);
  138. likeCountElement.textContent = currentCount + 1;
  139. }
  140. function toggleReplyForm(event) {
  141. var comment = event.target.closest(".comment");
  142. var replyForm = comment.querySelector(".reply-form");
  143. replyForm.classList.toggle("hidden");
  144. }
  145. function addReply(event) {
  146. var replyTextarea = event.target.previousElementSibling;
  147. var replyContent = replyTextarea.value.trim();
  148. if (replyContent !== "") {
  149. var reply = document.createElement("div");
  150. reply.className = "comment";
  151. var author = document.createElement("div");
  152. author.className = "author";
  153. var avatar = document.createElement("img");
  154. avatar.src = "../img/profile photo.png"; // 替换为实际的头像图片路径
  155. var authorName = document.createElement("span");
  156. authorName.textContent = "匿名用户";
  157. author.appendChild(avatar);
  158. author.appendChild(authorName);
  159. var actions = document.createElement("div");
  160. actions.className = "actions";
  161. var likeButton = document.createElement("button");
  162. likeButton.textContent = "点赞";
  163. likeButton.addEventListener("click", incrementLikeCount);
  164. var likeCount = document.createElement("span");
  165. likeCount.className = "like-count";
  166. likeCount.textContent = "0";
  167. actions.appendChild(likeButton);
  168. actions.appendChild(likeCount);
  169. var timestamp = document.createElement("div");
  170. timestamp.className = "timestamp";
  171. timestamp.textContent = new Date().toLocaleString();
  172. var content = document.createElement("div");
  173. content.className = "content";
  174. content.textContent = replyContent;
  175. reply.appendChild(author);
  176. reply.appendChild(actions);
  177. reply.appendChild(timestamp);
  178. reply.appendChild(content);
  179. var comment = event.target.closest(".comment");
  180. var replyForm = comment.querySelector(".reply-form");
  181. comment.insertBefore(reply, replyForm);
  182. var replyCountElement = comment.querySelector(".reply-count");
  183. var currentCount = parseInt(replyCountElement.textContent);
  184. replyCountElement.textContent = currentCount + 1;
  185. replyTextarea.value = "";
  186. replyForm.classList.add("hidden");
  187. }
  188. }
  189. </script>
  190. </body>
  191. </html>