{"id":953,"date":"2025-12-25T15:46:17","date_gmt":"2025-12-25T07:46:17","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=953"},"modified":"2025-12-25T15:46:17","modified_gmt":"2025-12-25T07:46:17","slug":"html-tablecss%e5%ae%9e%e7%8e%b0%e5%8f%af%e7%bc%96%e8%be%91%e8%a1%a8%e6%a0%bc%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81-3","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/25\/html-tablecss%e5%ae%9e%e7%8e%b0%e5%8f%af%e7%bc%96%e8%be%91%e8%a1%a8%e6%a0%bc%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81-3\/","title":{"rendered":"html table+css\u5b9e\u73b0\u53ef\u7f16\u8f91\u8868\u683c\u7684\u793a\u4f8b\u4ee3\u7801\u00a0"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u5957<strong>\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c\u7684\u5b8c\u6574\u793a\u4f8b<\/strong>\uff0c\u6f14\u793a\u5982\u4f55\u7528<br>\ud83d\udc49 <strong>HTML table + CSS + \u539f\u751f JavaScript \u5b9e\u73b0\u53ef\u7f16\u8f91\u8868\u683c<\/strong><br>\u4e0d\u4f9d\u8d56\u4efb\u4f55\u6846\u67b6\uff0c\u9002\u5408\u5b66\u4e60 \/ \u540e\u53f0\u7cfb\u7edf \/ \u7b80\u5355\u7ba1\u7406\u9875\u9762\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">HTML Table + CSS \u5b9e\u73b0\u53ef\u7f16\u8f91\u8868\u683c\uff08\u793a\u4f8b\u4ee3\u7801\uff09<\/h1>\n\n\n\n<p>\u5b9e\u73b0\u6548\u679c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 \u53cc\u51fb\u5355\u5143\u683c\u5373\u53ef\u7f16\u8f91<\/li>\n\n\n\n<li>\u2705 \u56de\u8f66\u4fdd\u5b58 \/ \u5931\u7126\u4fdd\u5b58<\/li>\n\n\n\n<li>\u2705 \u6837\u5f0f\u6e05\u6670\u3001\u6613\u6269\u5c55<\/li>\n\n\n\n<li>\u2705 \u539f\u751f JS\uff0c\u65e0\u4f9d\u8d56<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u6838\u5fc3\u601d\u8def\uff08\u5148\u770b\u539f\u7406\uff09<\/h2>\n\n\n\n<p>\u5b9e\u73b0\u53ef\u7f16\u8f91\u8868\u683c\uff0c\u4e00\u822c\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>contenteditable<\/code>\uff08\u6700\u7b80\u5355\uff09<\/strong> \u2b50 \u63a8\u8350<\/li>\n\n\n\n<li>\u70b9\u51fb\u540e\u628a <code>&lt;td><\/code> \u66ff\u6362\u6210 <code>&lt;input><\/code><\/li>\n<\/ol>\n\n\n\n<p>\u672c\u6587\u91c7\u7528 <strong>\u65b9\u6848\u4e00\uff08\u4e3b\u6d41 &amp; \u7b80\u6d01\uff09<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801\uff08\u53ef\u76f4\u63a5\u8fd0\u884c\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 HTML \u7ed3\u6784\uff08table\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE html&gt;\n&amp;lt;html lang=&quot;zh-CN&quot;&gt;\n&amp;lt;head&gt;\n  &amp;lt;meta charset=&quot;UTF-8&quot; \/&gt;\n  &amp;lt;title&gt;\u53ef\u7f16\u8f91\u8868\u683c\u793a\u4f8b&amp;lt;\/title&gt;\n  &amp;lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; \/&gt;\n&amp;lt;\/head&gt;\n&amp;lt;body&gt;\n\n&amp;lt;table class=&quot;editable-table&quot;&gt;\n  &amp;lt;thead&gt;\n    &amp;lt;tr&gt;\n      &amp;lt;th&gt;\u59d3\u540d&amp;lt;\/th&gt;\n      &amp;lt;th&gt;\u5e74\u9f84&amp;lt;\/th&gt;\n      &amp;lt;th&gt;\u57ce\u5e02&amp;lt;\/th&gt;\n    &amp;lt;\/tr&gt;\n  &amp;lt;\/thead&gt;\n  &amp;lt;tbody&gt;\n    &amp;lt;tr&gt;\n      &amp;lt;td contenteditable=&quot;true&quot;&gt;\u5f20\u4e09&amp;lt;\/td&gt;\n      &amp;lt;td contenteditable=&quot;true&quot;&gt;25&amp;lt;\/td&gt;\n      &amp;lt;td contenteditable=&quot;true&quot;&gt;\u5317\u4eac&amp;lt;\/td&gt;\n    &amp;lt;\/tr&gt;\n    &amp;lt;tr&gt;\n      &amp;lt;td contenteditable=&quot;true&quot;&gt;\u674e\u56db&amp;lt;\/td&gt;\n      &amp;lt;td contenteditable=&quot;true&quot;&gt;30&amp;lt;\/td&gt;\n      &amp;lt;td contenteditable=&quot;true&quot;&gt;\u4e0a\u6d77&amp;lt;\/td&gt;\n    &amp;lt;\/tr&gt;\n  &amp;lt;\/tbody&gt;\n&amp;lt;\/table&gt;\n\n&amp;lt;script src=&quot;table.js&quot;&gt;&amp;lt;\/script&gt;\n&amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 CSS \u6837\u5f0f\uff08style.css\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbody {\n  font-family: Arial, Helvetica, sans-serif;\n  padding: 30px;\n}\n\n.editable-table {\n  width: 60%;\n  border-collapse: collapse;\n}\n\n.editable-table th,\n.editable-table td {\n  border: 1px solid #ccc;\n  padding: 10px;\n  text-align: center;\n}\n\n.editable-table th {\n  background-color: #f5f5f5;\n}\n\n\/* \u53ef\u7f16\u8f91\u5355\u5143\u683c\u6837\u5f0f *\/\n.editable-table td&#x5B;contenteditable=&quot;true&quot;] {\n  background-color: #fff;\n  cursor: pointer;\n}\n\n\/* \u7f16\u8f91\u4e2d\u9ad8\u4eae *\/\n.editable-table td&#x5B;contenteditable=&quot;true&quot;]:focus {\n  outline: none;\n  background-color: #e6f7ff;\n  border-color: #1890ff;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 JavaScript\uff08table.js\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst cells = document.querySelectorAll(\n  &#039;.editable-table td&#x5B;contenteditable=&quot;true&quot;]&#039;\n);\n\ncells.forEach(cell =&gt; {\n  let oldValue = &#039;&#039;;\n\n  \/\/ \u83b7\u5f97\u7126\u70b9\u65f6\uff0c\u8bb0\u5f55\u65e7\u503c\n  cell.addEventListener(&#039;focus&#039;, () =&gt; {\n    oldValue = cell.innerText;\n  });\n\n  \/\/ \u5931\u53bb\u7126\u70b9\u65f6\uff0c\u4fdd\u5b58\u6570\u636e\n  cell.addEventListener(&#039;blur&#039;, () =&gt; {\n    const newValue = cell.innerText;\n\n    if (oldValue !== newValue) {\n      console.log(&#039;\u6570\u636e\u5df2\u4fee\u6539\uff1a&#039;, newValue);\n\n      \/\/ TODO\uff1a\u5728\u8fd9\u91cc\u8c03\u7528\u63a5\u53e3\uff0c\u63d0\u4ea4\u4fee\u6539\u540e\u7684\u6570\u636e\n    }\n  });\n\n  \/\/ \u56de\u8f66\u952e\u4fdd\u5b58\u5e76\u9000\u51fa\u7f16\u8f91\n  cell.addEventListener(&#039;keydown&#039;, e =&gt; {\n    if (e.key === &#039;Enter&#039;) {\n      e.preventDefault();\n      cell.blur();\n    }\n  });\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u6548\u679c\u8bf4\u660e<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\uddb1 <strong>\u5355\u51fb \/ \u53cc\u51fb\u5355\u5143\u683c<\/strong> \u2192 \u8fdb\u5165\u7f16\u8f91<\/li>\n\n\n\n<li>\u2328 <strong>\u56de\u8f66\u952e<\/strong> \u2192 \u4fdd\u5b58\u5e76\u9000\u51fa<\/li>\n\n\n\n<li>\ud83d\uddb1 <strong>\u70b9\u51fb\u5176\u4ed6\u533a\u57df<\/strong> \u2192 \u81ea\u52a8\u4fdd\u5b58<\/li>\n\n\n\n<li>\ud83c\udfa8 \u7f16\u8f91\u65f6\u5355\u5143\u683c\u9ad8\u4eae<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u8fdb\u9636\uff1a\u53ea\u5141\u8bb8\u7f16\u8f91\u6307\u5b9a\u5217<\/h2>\n\n\n\n<p>\u4f8b\u5982\u53ea\u5141\u8bb8\u7f16\u8f91\u300c\u5e74\u9f84\u300d\u5217\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;td contenteditable=&quot;true&quot; data-field=&quot;age&quot;&gt;25&amp;lt;\/td&gt;\n\n<\/pre><\/div>\n\n\n<p>JS \u4e2d\u8bfb\u53d6\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst field = cell.dataset.field;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u8fdb\u9636\uff1a\u6570\u636e\u63d0\u4ea4\u5230\u540e\u7aef\uff08\u793a\u610f\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfetch(&#039;\/api\/update&#039;, {\n  method: &#039;POST&#039;,\n  headers: { &#039;Content-Type&#039;: &#039;application\/json&#039; },\n  body: JSON.stringify({\n    field: field,\n    value: newValue\n  })\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5e38\u89c1\u5751\u4f4d\u63d0\u9192\uff08\u5f88\u91cd\u8981\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2757 1. contenteditable \u4f1a\u5141\u8bb8\u7c98\u8d34 HTML<\/h3>\n\n\n\n<p>\u89e3\u51b3\uff08\u53ea\u4fdd\u7559\u7eaf\u6587\u672c\uff09\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncell.addEventListener(&#039;paste&#039;, e =&gt; {\n  e.preventDefault();\n  const text = e.clipboardData.getData(&#039;text\/plain&#039;);\n  document.execCommand(&#039;insertText&#039;, false, text);\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2757 2. \u56de\u8f66\u4f1a\u6362\u884c<\/h3>\n\n\n\n<p>\u5df2\u5728\u793a\u4f8b\u4e2d\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ne.preventDefault();\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2757 3. \u6570\u5b57\u6821\u9a8c<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (cell.dataset.type === &#039;number&#039; &amp;amp;&amp;amp; isNaN(newValue)) {\n  alert(&#039;\u8bf7\u8f93\u5165\u6570\u5b57&#039;);\n  cell.innerText = oldValue;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u7b2c\u4e8c\u79cd\u65b9\u6848\uff08input \u7f16\u8f91\uff0c\u4e86\u89e3\u5373\u53ef\uff09<\/h2>\n\n\n\n<p>\u903b\u8f91\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u70b9\u51fb td<\/li>\n\n\n\n<li>\u521b\u5efa input<\/li>\n\n\n\n<li>\u66ff\u6362 td \u5185\u5bb9<\/li>\n\n\n\n<li>\u5931\u7126\u4fdd\u5b58<\/li>\n<\/ol>\n\n\n\n<p>\u9002\u5408\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e25\u683c\u8868\u5355\u6821\u9a8c<\/li>\n\n\n\n<li>\u5927\u578b\u540e\u53f0\u7cfb\u7edf<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u6211\u4e5f\u53ef\u4ee5\u7ed9\u4f60\u5b8c\u6574\u793a\u4f8b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u603b\u7ed3\u4e00\u53e5\u8bdd<\/h2>\n\n\n\n<p>\u2714 <code>contenteditable<\/code> \u662f\u5b9e\u73b0\u53ef\u7f16\u8f91\u8868\u683c\u6700\u5feb\u65b9\u5f0f<br>\u2714 CSS \u8d1f\u8d23\u4ea4\u4e92\u4f53\u9a8c\uff0cJS \u8d1f\u8d23\u6570\u636e\u903b\u8f91<br>\u2714 \u53ef\u8f7b\u677e\u5bf9\u63a5\u540e\u7aef\u63a5\u53e3<br>\u2714 \u975e\u5e38\u9002\u5408\u540e\u53f0 CRUD \u573a\u666f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u5957\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c\u7684\u5b8c\u6574\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7528\ud83d\udc49 HTML table + C&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/25\/html-tablecss%e5%ae%9e%e7%8e%b0%e5%8f%af%e7%bc%96%e8%be%91%e8%a1%a8%e6%a0%bc%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81-3\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-953","post","type-post","status-publish","format-standard","hentry","category-html5"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/953\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/953\/revisions\/954"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}