欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

固定导航栏|可编辑表格

程序员文章站 2022-03-07 19:40:13
...

固定导航栏|可编辑表格

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <style>
  8. body{
  9. margin: 0;
  10. padding: 0;
  11. height: 1024px;
  12. }
  13. nav{
  14. margin-top: 40px;
  15. position: relative;
  16. width: 100vw;
  17. height: 40px;
  18. background-color: #90D7EC;
  19. text-align: center;
  20. line-height: 40px;
  21. z-index: 9;
  22. }
  23. table{
  24. margin:100px auto;
  25. width: 500px;
  26. border-collapse: collapse;
  27. }
  28. tr,th,td{
  29. border: 1px solid green;
  30. margin: 0;
  31. padding: 0;
  32. text-align: center;
  33. }
  34. td>input{
  35. height: 100%;
  36. width: 80%;
  37. border: none;
  38. outline: none;
  39. }
  40. </style>
  41. <body>
  42. <nav id="navObj">固定导航栏</nav>
  43. <table>
  44. <tr>
  45. <th>名称</th>
  46. <th>内容</th>
  47. </tr>
  48. <tr>
  49. <td>
  50. </td>
  51. <td>
  52. 学习家园
  53. </td>
  54. </tr>
  55. <tr>
  56. <td>
  57. </td>
  58. <td>
  59. 学习家园
  60. </td>
  61. </tr>
  62. </table>
  63. </body>
  64. <script>
  65. var tdObj = document.getElementsByTagName('td');
  66. for(let i=0;i<tdObj.length;i++){
  67. tdObj[i].addEventListener('click',function(){
  68. edit(this)
  69. })
  70. }
  71. function edit(i) {
  72. var inputlen = document.getElementsByTagName('input').length
  73. var oldValue = i.innerHTML;
  74. if (inputlen ==1){
  75. var textBox = document.createElement("INPUT");
  76. textBox.type = "text";
  77. textBox.value = i.innerHTML;
  78. var timer;
  79. i.innerHTML = '';
  80. i.appendChild(textBox)
  81. textBox.select();
  82. setTimeout(function () {
  83. timer =textBox.onblur =function () {
  84. i.innerHTML = textBox.value
  85. }
  86. },2000)
  87. textBox.onkeydown = function (e) {
  88. if (e.keyCode == 13){
  89. clearTimeout(timer);
  90. i.innerHTML= textBox.value;
  91. }
  92. if (e.keyCode ==27){
  93. clearTimeout(timer)
  94. i.innerHTML = oldValue;
  95. }
  96. }
  97. }
  98. }
  99. var navObj = document.getElementById('navObj');
  100. window.onscroll = function () {
  101. if ( document.documentElement.scrollTop>40){
  102. navObj.style.top = (document.documentElement.scrollTop-40)+ 'px';
  103. }
  104. }
  105. </script>
  106. </html>