博客列表 >留言板实践代码

留言板实践代码

千里马遇伯乐
千里马遇伯乐原创
2022年04月04日 18:33:09558浏览

留言板HTML

  1. <p><br/></p><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>留言板</title><style type="text/css">

css部分

  1. *{
  2. margin: 0px;
  3. padding: 0px;
  4. box-sizing: border-box;
  5. }
  6. @media screen and (min-width: 400px ){
  7. body{
  8. text-align:center;
  9. width: 500px;
  10. margin-left: 40%;
  11. margin-right:40%;
  12. }
  13. }
  14. div{
  15. margin: 0 auto;
  16. margin-right: 0.3rem;
  17. margin-left: 0.3rem;
  18. right: 20rem;
  19. width: 25fr;
  20. height: 30rem;
  21. background-color: aqua;
  22. text-align: center;
  23. margin-top: 0.9rem;
  24. padding-top: 0.3rem;
  25. }
  26. div>p{
  27. padding-top: 0.5rem;
  28. margin-top: 0.2rem;
  29. letter-spacing: 5px;
  30. }
  31. ul>li{
  32. display: grid;
  33. list-style: none;
  34. font-size: 0.16rem;
  35. text-align: left;
  36. margin-left: 0.3rem;
  37. }
  38. ul{
  39. margin-top: 0.5rem;
  40. }
  41. input{
  42. margin-top:1rem;
  43. width: 80%;
  44. height:1.5rem;
  45. padding-left:left 5rem;
  46. border-radius: 0.1px;
  47. border: 0rem;
  48. }
  49. button{
  50. color: blue;
  51. border: 0px;
  52. background-color:aqua;
  53. margin-left:80% ;
  54. }</style><div><p>留言板</p><hr/><input type="text" onkeydown="addMgs(this)" placeholder="请输入内容"/><ul class="list"></ul></div>

JS部分

  1. <script>function addMgs(ele){
  2. if (event.key==='Enter'){
  3. const ul=document.querySelector('.list');
  4. if (ele.value.trim().length===0){
  5. alert('留言不能为空');
  6. ele.focus();
  7. return false;
  8. }
  9. // 添加一条新留言
  10. const li=document.createElement('li');
  11. li.innerHTML = ele.value + '<button onclick="del(this.parentNode)">删除</button>';
  12. ul.insertAdjacentElement('afterBegin',li);
  13. ele.value=null;
  14. ele.focus();
  15. }
  16. }
  17. // 删除
  18. function del(ele){
  19. return confirm('是否删除')?ele.remove():false;
  20. }
  21. </script>
  22. <!--!doctype-->

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