{"id":157122,"date":"2024-05-08T12:25:18","date_gmt":"2024-05-08T04:25:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/157122.html"},"modified":"2024-05-08T12:25:22","modified_gmt":"2024-05-08T04:25:22","slug":"%e4%bd%bf%e7%94%a8javascript%e9%9a%90%e8%97%8f%e4%b8%80%e4%b8%aatable%e7%9a%84%e6%9f%90%e4%ba%9b%e8%a1%8c%ef%bc%8c%e6%b1%82%e9%ab%98%e6%95%88%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/157122.html","title":{"rendered":"\u4f7f\u7528javascript\u9690\u85cf\u4e00\u4e2atable\u7684\u67d0\u4e9b\u884c\uff0c\u6c42\u9ad8\u6548\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26215629\/59782d4e-7910-4139-a93d-aa7eedb5a8c0.webp\" alt=\"\u4f7f\u7528javascript\u9690\u85cf\u4e00\u4e2atable\u7684\u67d0\u4e9b\u884c\uff0c\u6c42\u9ad8\u6548\u65b9\u6cd5\" \/><\/p>\n<p><p><strong>\u9ad8\u6548\u5730\u4f7f\u7528JavaScript\u9690\u85cftable\u7684\u67d0\u4e9b\u884c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528CSS\u7c7b\u4e0eJavaScript\u7ed3\u5408\u8fdb\u884c\u64cd\u4f5c\u3001\u91c7\u7528document.querySelectorAll\u6216getElementById\u9009\u62e9\u5668\u3001\u5229\u7528display\u5c5e\u6027\u8fdb\u884c\u5207\u6362\u3001\u4f7f\u7528\u4e8b\u4ef6\u59d4\u6258\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/strong> \u901a\u8fc7CSS\u7c7b\u8fdb\u884c\u63a7\u5236\u662f\u6700\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u6d4f\u89c8\u5668\u4f18\u5316\u4e86\u7c7b\u7684\u64cd\u4f5c\u6027\u80fd\uff0c\u5e76\u4e14JavaScript\u5bf9\u4e8e\u7c7b\u7684\u64cd\u4f5c\u975e\u5e38\u7075\u6d3b\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528CSS\u7c7b\u7ed3\u5408JavaScript\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aCSS\u7c7b\uff0c\u8be5\u7c7b\u7684<code>display<\/code>\u5c5e\u6027\u88ab\u8bbe\u7f6e\u4e3a<code>none<\/code>\uff0c\u5f53\u5e94\u7528\u8fd9\u4e2a\u7c7b\u5230table\u884c\u65f6\uff0c\u884c\u4f1a\u88ab\u9690\u85cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">.hidden {<\/p>\n<p>  display: none;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528JavaScript\u9009\u62e9\u60f3\u8981\u9690\u85cf\u7684\u884c\uff0c\u5e76\u7ed9\u8fd9\u4e9b\u884c\u6dfb\u52a0\u4e0a\u8ff0CSS\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function hideRows(rowIndices) {<\/p>\n<p>  rowIndices.forEach(function(rowIndex) {<\/p>\n<p>    var rows = document.querySelectorAll(&#039;table tr&#039;);<\/p>\n<p>    if (rows[rowIndex]) {<\/p>\n<p>      rows[rowIndex].classList.add(&#039;hidden&#039;);<\/p>\n<p>    }<\/p>\n<p>  });<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528querySelectorAll\u6216getElementById\u9009\u62e9\u5668<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>document.querySelectorAll<\/code>\u6216<code>document.getElementById<\/code>\u9009\u62e9\u5668\u80fd\u51c6\u786e\u5feb\u901f\u5730\u5b9a\u4f4d\u5230\u7279\u5b9a\u7684table\u53ca\u5176\u884c\u5143\u7d20\u3002<code>querySelectorAll<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u9009\u62e9\u5668\uff0c\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u901a\u8fc7CSS\u9009\u62e9\u5668\u8bed\u6cd5\u83b7\u53d6\u5339\u914d\u7684\u5143\u7d20\u5217\u8868\uff0c\u800c<code>getElementById<\/code>\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5143\u7d20\u7684id\u5c5e\u6027\u5feb\u901f\u8bbf\u95ee\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function hideRowById(tableId, rowIndex) {<\/p>\n<p>  var table = document.getElementById(tableId);<\/p>\n<p>  var rows = table.querySelectorAll(&#039;tr&#039;);<\/p>\n<p>  if (rows[rowIndex]) {<\/p>\n<p>    rows[rowIndex].classList.add(&#039;hidden&#039;);<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528display\u5c5e\u6027\u8fdb\u884c\u5207\u6362<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528CSS\u7c7b\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5cDOM\u5143\u7d20\u7684<code>style<\/code>\u5c5e\u6027\uff0c\u5c06\u7279\u5b9a\u884c\u7684<code>display<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a<code>none<\/code>\u5373\u53ef\u9690\u85cf\u8be5\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function toggleRowDisplay(row) {<\/p>\n<p>  if (row.style.display === &#039;none&#039;) {<\/p>\n<p>    row.style.display = &#039;&#039;;<\/p>\n<p>  } else {<\/p>\n<p>    row.style.display = &#039;none&#039;;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u76f4\u89c2\u4e14\u6613\u4e8e\u5b9e\u73b0\uff0c\u4f46\u5982\u679c\u9700\u8981\u9891\u7e41\u64cd\u4f5c\u5927\u91cfDOM\u5143\u7d20\uff0c\u4f1a\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u4e8b\u4ef6\u59d4\u6258\u63d0\u5347\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u4e8b\u4ef6\u59d4\u6258\u662f\u4e00\u79cd\u9ad8\u6548\u5904\u7406\u4e8b\u4ef6\u7684\u6280\u672f\uff0c\u901a\u8fc7\u5728\u7236\u5143\u7d20\u4e0a\u76d1\u542c\u4e8b\u4ef6\u6765\u7ba1\u7406\u591a\u4e2a\u5b50\u5143\u7d20\u7684\u4e8b\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u4e8e\u64cd\u4f5ctable\u884c\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u51cf\u5c11\u4e8b\u4ef6\u76d1\u542c\u5668\u7684\u6570\u91cf\uff0c\u4ece\u800c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var table = document.querySelector(&#039;table&#039;);<\/p>\n<p>table.addEventListener(&#039;click&#039;, function(event) {<\/p>\n<p>  var target = event.target;<\/p>\n<p>  while (target &amp;&amp; target.nodeName !== &#039;TR&#039;) {<\/p>\n<p>    target = target.parentElement;<\/p>\n<p>  }<\/p>\n<p>  if (target) {<\/p>\n<p>    target.classList.toggle(&#039;hidden&#039;);<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4e3a\u6574\u4e2atable\u8bbe\u7f6e\u4e86\u4e00\u4e2aclick\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u5e76\u5728\u5185\u90e8\u68c0\u67e5\u88ab\u70b9\u51fb\u7684\u76ee\u6807\u5143\u7d20\u662f\u5426\u4e3atable\u884c\u5143\u7d20\uff0c\u5982\u679c\u662f\uff0c\u5219\u5207\u6362\u5176\u53ef\u89c1\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u7528JavaScript\u9690\u85cftable\u7684\u7279\u5b9a\u884c\uff0c\u540c\u65f6\u8fd8\u80fd\u4fdd\u8bc1\u64cd\u4f5c\u7684\u9ad8\u6548\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u8981\u9009\u53d6\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8fbe\u5230\u9884\u671f\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528JavaScript\u9690\u85cfTable\u4e2d\u7684\u7279\u5b9a\u884c\uff1f<\/strong><\/p>\n<ul>\n<li><strong>\u95ee\u9898\uff1a\u5982\u4f55\u4f7f\u7528JavaScript\u52a8\u6001\u9690\u85cfTable\u7684\u7279\u5b9a\u884c\uff1f<\/strong>\n<ul>\n<li>\u89e3\u7b54\uff1a\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528JavaScript\u52a8\u6001\u9690\u85cfTable\u4e2d\u7684\u7279\u5b9a\u884c\uff1a\n<ul>\n<li>\u9996\u5148\uff0c\u901a\u8fc7JavaScript\u83b7\u53d6\u5bf9Table\u7684\u5f15\u7528\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528<code>rows<\/code>\u5c5e\u6027\u8bbf\u95eeTable\u7684\u884c\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u904d\u5386\u884c\uff0c\u4f7f\u7528<code>style.display<\/code>\u5c5e\u6027\u5c06\u8981\u9690\u85cf\u7684\u884c\u8bbe\u7f6e\u4e3a<code>&quot;none&quot;<\/code>\u3002<\/li>\n<li>\u6700\u540e\uff0c\u5904\u7406\u9690\u85cf\u7684\u884c\uff0c\u4f8b\u5982\u8bbe\u7f6e\u89e6\u53d1\u5668\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u7684\u65f6\u5019\u91cd\u65b0\u663e\u793a\u5b83\u4eec\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u9ad8\u6548\u5730\u9690\u85cfTable\u4e2d\u7684\u591a\u4e2a\u884c\uff1f<\/strong><\/p>\n<ul>\n<li><strong>\u95ee\u9898\uff1a\u5982\u4f55\u4f7f\u7528JavaScript\u9ad8\u6548\u5730\u9690\u85cfTable\u4e2d\u7684\u591a\u4e2a\u884c\uff1f<\/strong>\n<ul>\n<li>\u89e3\u7b54\uff1a\u82e5\u8981\u9ad8\u6548\u5730\u9690\u85cfTable\u4e2d\u7684\u591a\u4e2a\u884c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\n<ul>\n<li>\u9996\u5148\uff0c\u901a\u8fc7JavaScript\u83b7\u53d6\u5bf9Table\u7684\u5f15\u7528\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u8981\u9690\u85cf\u7684\u884c\u7684\u7d22\u5f15\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u5bf9\u5e94\u7684\u884c\u8bbe\u7f6e\u4e3a<code>&quot;none&quot;<\/code>\u3002<\/li>\n<li>\u6700\u540e\uff0c\u5904\u7406\u9690\u85cf\u7684\u884c\u4ee5\u53ca\u6839\u636e\u9700\u6c42\u8bbe\u7f6e\u663e\u793a\u5b83\u4eec\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u6709\u6ca1\u6709\u66f4\u5feb\u7684\u65b9\u6cd5\u6765\u9690\u85cfTable\u4e2d\u7684\u884c\uff1f<\/strong><\/p>\n<ul>\n<li><strong>\u95ee\u9898\uff1a\u662f\u5426\u6709\u66f4\u5feb\u7684\u65b9\u6cd5\u6765\u4f7f\u7528JavaScript\u9690\u85cfTable\u4e2d\u7684\u884c\uff1f<\/strong>\n<ul>\n<li>\u89e3\u7b54\uff1a\u662f\u7684\uff0c\u6709\u51e0\u79cd\u66f4\u5feb\u7684\u65b9\u6cd5\u6765\u9690\u85cfTable\u4e2d\u7684\u884c\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5176\u4e2d\u4e24\u79cd\uff1a\n<ul>\n<li>\u4f7f\u7528\u57fa\u4e8e\u7c7b\u540d\u6216\u6570\u636e\u5c5e\u6027\u7684\u9009\u62e9\u5668\uff1a\u901a\u8fc7\u4e3a\u8981\u9690\u85cf\u7684\u884c\u6dfb\u52a0\u7279\u5b9a\u7684\u7c7b\u540d\u6216\u6570\u636e\u5c5e\u6027\uff0c\u7136\u540e\u4f7f\u7528CSS\u9009\u62e9\u5668\u6548\u7387\u66f4\u9ad8\u5730\u9009\u62e9\u8fd9\u4e9b\u884c\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e<code>display: none<\/code>\u6765\u9690\u85cf\u5b83\u4eec\u3002<\/li>\n<li>\u4f7f\u7528\u7f13\u5b58\u5f15\u7528\uff1a\u5c06\u5bf9Table\u548c\u884c\u7684\u5f15\u7528\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u907f\u514d\u5728\u6bcf\u6b21\u9690\u85cf\u8fc7\u7a0b\u4e2d\u91cd\u65b0\u67e5\u627e\u8fd9\u4e9b\u5143\u7d20\u3002\u8fd9\u4f1a\u51cf\u5c11\u5bf9DOM\u7684\u591a\u6b21\u8bbf\u95ee\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4ee5\u4e0a\u65b9\u6cd5\u53ea\u662f\u4e00\u4e9b\u793a\u4f8b\uff0c\u5e76\u4e0d\u662f\u552f\u4e00\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6839\u636e\u7279\u5b9a\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u60a8\u5e94\u7528\u7a0b\u5e8f\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u9ad8\u6548\u5730\u4f7f\u7528JavaScript\u9690\u85cftable\u7684\u67d0\u4e9b\u884c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528CSS\u7c7b\u4e0eJavaScript\u7ed3 [&hellip;]","protected":false},"author":3,"featured_media":157128,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/157122"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=157122"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/157122\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/157128"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=157122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=157122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=157122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}