html 评论区创建

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>李佳的网页菜鸟之路</title>
 </head>
 <body><!-- form是指表单,也可以理解成一块区域  action属性指将表单提交的地址 method属性指提交方式 现在流行的为post以及get(post更为保密)-->
  <form action="#" method="post">
  评论:<br/> <!--clos指每行可以显示60个字符 rows指行数-->
  <textarea cols="60" rows="5">  评论时注意文明用语。
  </textarea><!--textarea指可以创建多行文本-->
  <br/><br/><!-- input指表单控件,type指向的是控件类型,例如下行中submit(中文意思为顺从)指提交按钮控件 value(中文意思为价值价格重要性)指控件上显示的名字-->
  <input type="submit" value="提交"/>
  </form>
 </body>
</html>


  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个HTML评论页面一般需要以下步骤: 1. 创建HTML结构 在HTML中,评论可以使用`<div>`元素来包含所有的评论,并使用`<ul>`元素和`<li>`元素来分别表示评论列表和每条评论。此外,评论还需要一个表单来允许用户输入评论。可以使用`<form>`元素来创建表单。 ```html <div class="comments"> <ul class="comment-list"> <li class="comment"> <div class="comment-body"> <p class="comment-author">John Doe</p> <p class="comment-content">This is a comment.</p> </div> </li> <!-- more comments... --> </ul> <form class="comment-form"> <h2>Leave a Comment</h2> <input type="text" name="author" placeholder="Name" required> <textarea name="content" placeholder="Comment" required></textarea> <button type="submit">Submit</button> </form> </div> ``` 2. 添加CSS样式 为了让评论看起来更好,我们需要添加一些CSS样式。可以使用`<style>`元素在HTML文件中添加CSS样式,或将样式写入独立的CSS文件中并在HTML文件中引用。 ```css .comments { max-width: 600px; margin: 0 auto; } .comment-list { list-style-type: none; padding: 0; } .comment { margin-bottom: 20px; border: 1px solid #ccc; border-radius: 5px; padding: 10px; } .comment-author { font-weight: bold; } .comment-content { margin-top: 10px; } .comment-form { margin-top: 20px; } .comment-form h2 { margin-bottom: 10px; } .comment-form input, .comment-form textarea { display: block; width: 100%; margin-bottom: 10px; padding: 5px; border: 1px solid #ccc; border-radius: 5px; } .comment-form button[type="submit"] { padding: 5px 10px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; } ``` 3. 实现JavaScript交互 为了使表单能够提交评论,需要使用JavaScript和服务器端代码来处理表单提交。在这里,我们可以使用AJAX技术来异步提交表单数据,以避免页面刷新。 ```js // 获取表单元素 var form = document.querySelector('.comment-form'); // 监听表单提交事件 form.addEventListener('submit', function(event) { event.preventDefault(); // 阻止表单默认提交行为 // 获取表单数据 var author = form.elements.author.value; var content = form.elements.content.value; // 发送POST请求 var xhr = new XMLHttpRequest(); xhr.open('POST', '/comment'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = function() { if (xhr.status === 200) { // 成功提交评论,刷新评论列表 fetchComments(); } else { alert('Failed to submit comment.'); } }; xhr.send(JSON.stringify({ author: author, content: content })); }); // 获取评论列表 function fetchComments() { var xhr = new XMLHttpRequest(); xhr.open('GET', '/comments'); xhr.onload = function() { if (xhr.status === 200) { // 成功获取评论,更新评论列表 var comments = JSON.parse(xhr.responseText); var commentList = document.querySelector('.comment-list'); commentList.innerHTML = ''; comments.forEach(function(comment) { var li = document.createElement('li'); li.classList.add('comment'); var div = document.createElement('div'); div.classList.add('comment-body'); var author = document.createElement('p'); author.classList.add('comment-author'); author.textContent = comment.author; var content = document.createElement('p'); content.classList.add('comment-content'); content.textContent = comment.content; div.appendChild(author); div.appendChild(content); li.appendChild(div); commentList.appendChild(li); }); } else { alert('Failed to fetch comments.'); } }; xhr.send(); } // 初始加载评论列表 fetchComments(); ``` 以上就是实现HTML评论页面的基本步骤。当然,实际场景中还需要处理很多细节和安全性问题,例如输入验证、防止XSS攻击等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值