Blogger Information
Blog 70
fans 1
comment 0
visits 53040
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
固定导航栏和可编辑表格
葡萄枝子
Original
657 people have browsed it

固定导航栏和可编辑表格

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. body, ul, li {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. ul {
  12. list-style: none;
  13. }
  14. a {
  15. text-decoration: none;
  16. }
  17. a:hover {
  18. text-decoration: underline;
  19. }
  20. .container {
  21. width: 600px;
  22. margin: 20px auto;
  23. background-color: #fbfbfb;
  24. }
  25. nav > div {
  26. height: 90px;
  27. display: flex;
  28. justify-content: space-between;
  29. align-items: center;
  30. border: 1px solid #ccc;
  31. padding: 0 20px;
  32. }
  33. nav > ul {
  34. height: 30px;
  35. width: 580px;
  36. background-color: #fbfbfb;
  37. border-bottom: 1px solid #888888;
  38. display: flex;
  39. justify-content: space-between;
  40. align-items: center;
  41. padding: 5px 10px;
  42. }
  43. main {
  44. height: 100vh;
  45. border: 1px solid #ccc;
  46. }
  47. main > table {
  48. border: 1px solid #ccc;
  49. margin: 15px auto;
  50. background-color: #fff;
  51. width: 95%;
  52. }
  53. main > table thead th:first-of-type {
  54. width: 10%;
  55. }
  56. main > table thead th:first-of-type ~ * {
  57. width: 45%;
  58. }
  59. main > table tbody input {
  60. width: 95%;
  61. margin: 0 1px;
  62. }
  63. main > table tr {
  64. border: 1px solid #ccc;
  65. background-color: aliceblue;
  66. }
  67. footer {
  68. height: 60px;
  69. line-height: 60px;
  70. border: 1px solid #ccc;
  71. text-align: center;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <header class="container">
  77. <nav>
  78. <div id="nav-top">
  79. <div>Logo</div>
  80. <div></div>
  81. <div></div>
  82. </div>
  83. <ul id="nav-menu">
  84. <li><a href="" title="">导航1</a></li>
  85. <li><a href="" title="">导航2</a></li>
  86. <li><a href="" title="">导航3</a></li>
  87. <li><a href="" title="">导航4</a></li>
  88. <li><a href="" title="">导航5</a></li>
  89. </ul>
  90. </nav>
  91. </header>
  92. <main class="container ">
  93. <table>
  94. <thead>
  95. <tr>
  96. <th>ID</th>
  97. <th>name</th>
  98. <th>value</th>
  99. </tr>
  100. </thead>
  101. <tbody>
  102. <tr>
  103. <td>1</td>
  104. <td>text name</td>
  105. <td>text value</td>
  106. </tr>
  107. <tr>
  108. <td>2</td>
  109. <td>text name2</td>
  110. <td>text value2</td>
  111. </tr>
  112. </tbody>
  113. </table>
  114. </main>
  115. <footer class="container">
  116. @copy 2021
  117. </footer>
  118. <script>
  119. // 固定导航
  120. var navTop = document.getElementById('nav-top'),
  121. navMenu = document.getElementById('nav-menu');
  122. window.onscroll = function () {
  123. if (document.documentElement.scrollTop > navTop.offsetHeight + navTop.offsetTop) {
  124. navMenu.style.cssText = 'position:fixed;top:0';
  125. } else {
  126. navMenu.style.cssText = 'position:static';
  127. }
  128. }
  129. // 可编辑表格
  130. // var tds = document.getElementsByTagName('table')[0].getElementsByTagName('td');
  131. // [...tds].forEach(function (td){
  132. // // ID字段不可编辑
  133. // if (td !== [...td.parentNode.childNodes].filter(el => el.nodeType === 1).shift()) {
  134. // td.onclick = function () {
  135. // // 如果input框,直接获取焦点
  136. // if ([...this.childNodes].some(el => el.nodeName === 'INPUT')) {
  137. // [...this.childNodes].filter(el => el.nodeName === 'INPUT').shift().focus();
  138. // } else {
  139. // var eInput = document.createElement('input');
  140. // eInput.type = 'text';
  141. // eInput.value = this.textContent;
  142. // this.innerHTML = '';
  143. // this.appendChild(eInput);
  144. // eInput.focus();
  145. // // 失去焦点时
  146. // eInput.onblur = function () {
  147. // this.parentNode.innerHTML = this.value;
  148. // }
  149. // }
  150. // }
  151. // }
  152. // });
  153. var tBody = document.getElementsByTagName('table')[0].getElementsByTagName('tbody')[0];
  154. tBody.addEventListener('click', function (ev) {
  155. var event = ev || window.event;
  156. event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true);
  157. // ID字段不可编辑
  158. if ([...event.target.parentNode.childNodes].filter(el => el.nodeType === 1).shift() !== event.target) {
  159. // 如果input框,直接获取焦点
  160. if ([...event.target.childNodes].some(el => el.nodeName === 'INPUT')) {
  161. [...event.target.childNodes].filter(el => el.nodeName === 'INPUT').shift().focus();
  162. } else {
  163. var eInput = document.createElement('input');
  164. eInput.type = 'text';
  165. eInput.value = event.target.textContent;
  166. event.target.innerHTML = '';
  167. event.target.appendChild(eInput);
  168. eInput.focus();
  169. // 失去焦点时
  170. eInput.onblur = function () {
  171. this.parentNode.innerHTML = this.value;
  172. }
  173. }
  174. }
  175. });
  176. </script>
  177. </body>
  178. </html>
  • 页面滚动时,导航栏固定在顶部
    固定导航栏

股东顶部

  • 编辑第一张图中表格最后一个字段为 hello world! 失去焦点生效

可编辑表格

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:关于Enter和Esc功能可以再完善一下
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments