如何用JavaScript编写留言板功能案例?

更新于
2026-09-26 19:37:32
3阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计356个文字,预计阅读时间需要2分钟。

如何用JavaScript编写留言板功能案例?

原文示例:留言板/* CSS样式代码 */

简化版:留言板/* 简化CSS样式 */

本文实例为大家分享了JavaScript实现留言板功能的具体代码,供大家参考,具体内容如下

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>留言板</title> <style type="text/css"> #div1{ width: 400px; height: 200px; background-color: antiquewhite; } span{ color: blue; text-decoration: underline; } </style> </head> <body> <div id="div1"> 姓名:<input type="text" name="user" id="username" /><br /> 内容:<textarea rows="10" cols="40" id="info"> </textarea> <input type="button" value="留言" id="btn" /><br /> </div> <h3>留言板</h3><br /> <div id="div2"> </div> </body> <script type="text/javascript"> var userInput = document.getElementById("username"); var infoInput = document.getElementById("info"); var btn = document.getElementById("btn"); var div2 = document.getElementById("div2"); btn.onclick = function(){ var user = userInput.value; var info = infoInput.value; var divUser = document.createElement("div"); divUser.innerText = user; divUser.style.backgroundColor = "darkgrey"; divUser.style.width = "400px"; divUser.style.height = "30px"; div2.appendChild(divUser); var divInfo = document.createElement("div"); divInfo.innerText = info; divInfo.style.backgroundColor = "antiquewhite"; divInfo.style.width = "400px"; divInfo.style.height = "80px"; div2.appendChild(divInfo); var del = document.createElement("span"); del.innerText = "删除"; del.style.float = "right"; divInfo.appendChild(del); del.onclick = function(){ divUser.remove(); divInfo.remove(); del.remove(); } } </script> </html>

showtime:

如何用JavaScript编写留言板功能案例?

点击删除,可以删除留言:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计356个文字,预计阅读时间需要2分钟。

如何用JavaScript编写留言板功能案例?

原文示例:留言板/* CSS样式代码 */

简化版:留言板/* 简化CSS样式 */

本文实例为大家分享了JavaScript实现留言板功能的具体代码,供大家参考,具体内容如下

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>留言板</title> <style type="text/css"> #div1{ width: 400px; height: 200px; background-color: antiquewhite; } span{ color: blue; text-decoration: underline; } </style> </head> <body> <div id="div1"> 姓名:<input type="text" name="user" id="username" /><br /> 内容:<textarea rows="10" cols="40" id="info"> </textarea> <input type="button" value="留言" id="btn" /><br /> </div> <h3>留言板</h3><br /> <div id="div2"> </div> </body> <script type="text/javascript"> var userInput = document.getElementById("username"); var infoInput = document.getElementById("info"); var btn = document.getElementById("btn"); var div2 = document.getElementById("div2"); btn.onclick = function(){ var user = userInput.value; var info = infoInput.value; var divUser = document.createElement("div"); divUser.innerText = user; divUser.style.backgroundColor = "darkgrey"; divUser.style.width = "400px"; divUser.style.height = "30px"; div2.appendChild(divUser); var divInfo = document.createElement("div"); divInfo.innerText = info; divInfo.style.backgroundColor = "antiquewhite"; divInfo.style.width = "400px"; divInfo.style.height = "80px"; div2.appendChild(divInfo); var del = document.createElement("span"); del.innerText = "删除"; del.style.float = "right"; divInfo.appendChild(del); del.onclick = function(){ divUser.remove(); divInfo.remove(); del.remove(); } } </script> </html>

showtime:

如何用JavaScript编写留言板功能案例?

点击删除,可以删除留言:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。