博客列表 >JS留言板实践

JS留言板实践

P粉317509817
P粉317509817原创
2022年04月04日 16:50:14475浏览

JS实现留言板功能

  • 功能1:可以实现留言的显示

  • 功能2:可以实现用户输入‘你好’、‘你吃了吗’这两句话,后台会自动回复

  • 功能3:双击留言可以实现删除留言共呢个

  • 功能4:实现enter键和‘Submit’键经行留言提交

html代码

  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. <link rel="stylesheet" href="留言板.css">
  9. <script src="留言板.js"></script>
  10. </head>
  11. <body>
  12. <div class="containt">
  13. <ul class="list"></ul>
  14. <div class="shurukaung">
  15. <input type="text" class="search" onkeydown="addMsg(this)" placeholder="请输入内容" autofocus />
  16. <button class="submit" onclick="submit()">Submit</button>
  17. </div>
  18. </div>
  19. </body>
  20. </html>

CSS代码

  1. * {
  2. /* 页面初始化 */
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. /* a标签初始化 */
  8. a {
  9. text-decoration: none;
  10. /* 去掉下划线 */
  11. color: #555;
  12. }
  13. body {
  14. background-color: rgb(243,245,247);
  15. }
  16. li {
  17. list-style:circle;
  18. /* 去掉无序列表前的小圆点 */
  19. }
  20. .containt {
  21. /* background-color: red; */
  22. width: 1000px;
  23. height:900px;
  24. margin: auto;
  25. display: grid;
  26. grid-template-rows:800px 100px;
  27. gap:40px;
  28. margin-top: 80px;
  29. }
  30. .shurukaung {
  31. width: 1000px;
  32. height:900px;
  33. margin: auto;
  34. display: grid;
  35. grid-template-columns: 800px 170px;
  36. gap: 10px ;
  37. place-content: space-between;
  38. /* background-color: red; */
  39. /* position:fixed; */
  40. /* margin-top:1000px; */
  41. /* position: relative;
  42. top:800px */
  43. }
  44. .shurukaung input {
  45. width: 800px;
  46. height: 50px;
  47. font-size: large;
  48. border-radius: 10px;
  49. border: 3px solid black;
  50. position: relative;
  51. }
  52. .shurukaung .submit {
  53. border-radius: 10px;
  54. background-color: #D0e1ca;
  55. font-size:x-large;
  56. font-weight:bolder;
  57. color:white;
  58. }
  59. input:focus{
  60. border-color: #66afe9;
  61. outline: 0;
  62. -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
  63. box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)
  64. }
  65. .shurukaung button:focus{
  66. border-color:#66afe9 ;
  67. outline: 0;
  68. -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
  69. box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)
  70. }
  71. /* .containt {
  72. background-color:red;
  73. height: 100px;
  74. } */
  75. .containt .list .item1,
  76. .containt .list .item {
  77. /* color:#00c5bd; */
  78. font-size:25px;
  79. font-weight: bold;
  80. display: grid;
  81. /* grid-auto-rows: 40px 100px; */
  82. }
  83. .containt .list .userName{
  84. width: 300px;
  85. background-color: #D0e1ca;
  86. color: black;
  87. font-size: smaller;
  88. border-radius: 10px 10px 0 0;
  89. padding: 5px;
  90. overflow: hidden;
  91. text-overflow: ellipsis;
  92. white-space: nowrap;
  93. }
  94. .containt .list .context {
  95. width: 300px;
  96. background-color: #d0d0d0;
  97. margin-bottom: 20px;
  98. border-radius: 0 0 10px 10px;
  99. padding-left: 10px;
  100. }
  101. .containt .list {
  102. overflow: auto ;
  103. }
  104. .containt .list .context1 {
  105. width: 300px;
  106. background-color: #d0d0d0;
  107. margin-bottom: 20px;
  108. border-radius: 0 0 10px 10px;
  109. padding-left: 10px;
  110. }
  111. .containt .list .item1 {
  112. place-content: end;
  113. }

js代码

  1. let i = 0;
  2. function addMsg(ele){
  3. if (event.key === 'Enter'){
  4. // console.log(ele);
  5. // 获取元素
  6. const ul = document.querySelector('.list');
  7. // 判断是否为空
  8. // 方法trim为去掉空格
  9. if (ele.value.trim().length === 0){
  10. alert('留言不能为空');
  11. ele.focus();
  12. return false;
  13. }
  14. else if (ele.value.trim().length >= 20){
  15. alert('不能超过20个字符');
  16. return false;
  17. }
  18. // 添加新留言
  19. const li = document.createElement('li');
  20. // li.style.color='red';
  21. li.innerHTML = `<div class="userName">用户${++i}:&nbsp; &nbsp;&nbsp;${ele.value}</div>
  22. <div class="context">${ele.value}</div>`;
  23. li.className = 'item';
  24. li.ondblclick = function(){
  25. return confirm('是否删除') ? li.parentNode.removeChild(li) : false;
  26. };
  27. // 将输入值转为html文字值
  28. ul.insertAdjacentElement('afterBegin',li);
  29. ele.value = null;
  30. ele.focus();
  31. // return ele;
  32. time();
  33. }
  34. }
  35. function submit(){
  36. const ul = document.querySelector('.list');
  37. const text = document.querySelector('.search');
  38. // console.log(typeof(text.value));
  39. if (text.value.trim().length === 0){
  40. alert('留言不能为空');
  41. text.focus();
  42. return false;
  43. }
  44. else if (text.value.trim().length >= 10){
  45. alert('不能超过三十个字符');
  46. return false;
  47. }
  48. const li = document.createElement('li');
  49. // // li.style.color='red';
  50. li.innerHTML = `<div class="userName">用户${++i}:&nbsp; &nbsp;&nbsp;${text.value}</div>
  51. <div class="context">${text.value}</div>`;
  52. li.className = 'item';
  53. li.ondblclick = function(){
  54. return confirm('是否删除') ? li.parentNode.removeChild(li) : false;
  55. };
  56. // // 将输入值转为html文字值
  57. ul.insertAdjacentElement('afterBegin',li);
  58. text.value = null;
  59. text.focus();
  60. }
  61. function time(){
  62. let x = true;
  63. if (x){
  64. setTimeout(function(){
  65. const ul = document.querySelector('.list');
  66. let ele1 = document.querySelector('.context').innerHTML;
  67. if (ele1.trim()==='你好'){
  68. const li = document.createElement('li');
  69. li.innerHTML=`<div class="userName">客服:</div>
  70. <div class="context1">你好</div>`;
  71. li.className = 'item1';
  72. ul.insertAdjacentElement('afterBegin',li);
  73. }
  74. else if (ele1.trim()==='你吃了吗'){
  75. const li = document.createElement('li');
  76. li.innerHTML=`<div class="userName">客服:</div>
  77. <div class="context1">我没吃</div>`;
  78. li.className = 'item1';
  79. ul.insertAdjacentElement('afterBegin',li);
  80. }
  81. return x=false;
  82. },1000)
  83. }
  84. }

效果演示:

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