{"id":169256,"date":"2024-05-08T16:42:19","date_gmt":"2024-05-08T08:42:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169256.html"},"modified":"2024-05-08T16:42:24","modified_gmt":"2024-05-08T08:42:24","slug":"javascript-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%9a%90%e8%97%8f%e4%b8%8b%e6%8b%89%e6%a1%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/169256.html","title":{"rendered":"javascript \u5982\u4f55\u5b9e\u73b0\u9690\u85cf\u4e0b\u62c9\u6846"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035651\/af93a41f-cc5a-47ef-9ae9-898abf7e7359.webp\" alt=\"javascript \u5982\u4f55\u5b9e\u73b0\u9690\u85cf\u4e0b\u62c9\u6846\" \/><\/p>\n<p><p>JavaScript \u5b9e\u73b0\u9690\u85cf\u4e0b\u62c9\u6846\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec<strong>\u8bbe\u7f6e<code>display<\/code>\u5c5e\u6027\u3001\u901a\u8fc7<code>visibility<\/code>\u5c5e\u6027\u3001\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3001\u5229\u7528<code>class<\/code>\u52a8\u6001\u63a7\u5236<\/strong>\u3002\u8bbe\u7f6e<code>display<\/code>\u5c5e\u6027\u4e3a<code>none<\/code>\u662f\u6700\u5e38\u89c1\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u4e0b\u62c9\u6846\u4e0d\u5360\u636e\u4efb\u4f55\u5e03\u5c40\u7a7a\u95f4\uff0c\u4ece\u800c\u5728\u89c6\u89c9\u4e0a\u5f7b\u5e95\u9690\u85cf\u4e0b\u62c9\u6846\u3002<\/p>\n<\/p>\n<p><p>\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>\u8bbe\u7f6e<code>display<\/code>\u5c5e\u6027\u4e3a<code>none<\/code><\/strong>\uff0c\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u5f0f\u6765\u9690\u85cf\u5143\u7d20\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0cJavaScript\u52a8\u6001\u5730\u6539\u53d8\u4e0b\u62c9\u6846\u7684<code>display<\/code> CSS\u5c5e\u6027\u503c\u4e3a<code>none<\/code>\u3002\u8fd9\u6837\uff0c\u4e0b\u62c9\u6846\u5c06\u4e0d\u518d\u663e\u793a\u5728\u9875\u9762\u4e0a\uff0c\u4e14\u4e0d\u5360\u7528\u4efb\u4f55\u7a7a\u95f4\u3002\u4e3a\u4e86\u91cd\u65b0\u663e\u793a\u4e0b\u62c9\u6846\uff0c\u53ea\u9700\u5c06<code>display<\/code>\u5c5e\u6027\u8bbe\u7f6e\u56de<code>block<\/code>\u6216<code>inline<\/code>\uff0c\u6216\u8005\u662f\u5b83\u6700\u521d\u7684\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u64cd\u4f5c\u7b80\u5355\u3001\u6613\u4e8e\u7406\u89e3\uff0c\u662f\u521d\u5b66\u8005\u5e38\u7528\u7684\u9690\u85cf\u5143\u7d20\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bbe\u7f6eDISPLAY\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7JavaScript\u8bbe\u7f6e\u5143\u7d20\u7684<code>display<\/code>\u5c5e\u6027\u4e3a<code>none<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5143\u7d20\u7684\u9690\u85cf\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u80fd\u591f\u9690\u85cf\u4e0b\u62c9\u6846\uff0c\u800c\u4e14\u5f53\u4e0b\u62c9\u6846\u88ab\u9690\u85cf\u540e\uff0c\u5728\u9875\u9762\u5e03\u5c40\u4e2d\u4e0d\u518d\u5360\u7528\u7a7a\u95f4\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u52a8\u6001\u8c03\u6574\u9875\u9762\u5e03\u5c40\u7684\u573a\u666f\u5c24\u4e3a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>display<\/code>\u5c5e\u6027\u9690\u85cf\u4e0b\u62c9\u6846\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u7684JavaScript\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function hideDropdown() {<\/p>\n<p>    document.getElementById(&quot;myDropdown&quot;).style.display = &quot;none&quot;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u518d\u6b21\u663e\u793a\u4e0b\u62c9\u6846\u65f6\uff0c\u53ea\u9700\u8981\u5c06<code>display<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a<code>block<\/code>\uff08\u6216<code>inline<\/code>, <code>inline-block<\/code>\u7b49\uff0c\u6839\u636e\u5b9e\u9645\u5e03\u5c40\u9700\u6c42\u800c\u5b9a\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function showDropdown() {<\/p>\n<p>    document.getElementById(&quot;myDropdown&quot;).style.display = &quot;block&quot;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7VISIBILITY\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u5f0f\u662f\u901a\u8fc7\u8bbe\u7f6e\u5143\u7d20\u7684<code>visibility<\/code>\u5c5e\u6027\u4e3a<code>hidden<\/code>\u6765\u9690\u85cf\u4e0b\u62c9\u6846\u3002\u4e0e<code>display:none<\/code>\u4e0d\u540c\uff0c\u4f7f\u7528<code>visibility:hidden<\/code>\u9690\u85cf\u7684\u5143\u7d20\u4ecd\u7136\u4f1a\u5360\u636e\u5728\u9875\u9762\u4e0a\u539f\u6765\u7684\u7a7a\u95f4\uff0c\u53ea\u662f\u5185\u5bb9\u4e0d\u53ef\u89c1\u3002<\/p>\n<\/p>\n<p><p>\u9690\u85cf\u548c\u663e\u793a\u4e0b\u62c9\u6846\u7684\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function hideDropdown() {<\/p>\n<p>    document.getElementById(&quot;myDropdown&quot;).style.visibility = &quot;hidden&quot;;<\/p>\n<p>}<\/p>\n<p>function showDropdown() {<\/p>\n<p>    document.getElementById(&quot;myDropdown&quot;).style.visibility = &quot;visible&quot;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5229\u7528<code>visibility<\/code>\u5c5e\u6027\u9690\u85cf\u4e0b\u62c9\u6846<\/strong>\u5bf9\u4e8e\u9700\u8981\u4fdd\u6301\u9875\u9762\u5e03\u5c40\u7a33\u5b9a\u7684\u573a\u666f\u8f83\u4e3a\u9002\u7528\uff0c\u56e0\u4e3a\u9690\u85cf\u5143\u7d20\u4e0d\u4f1a\u5bfc\u81f4\u9875\u9762\u5e03\u5c40\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528REMOVE()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u518d\u9700\u8981\u7684\u4e0b\u62c9\u6846\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u5c06\u5176\u76f4\u63a5\u4eceDOM\u4e2d\u5220\u9664\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u9690\u85cf\u5143\u7d20\u4e0d\u540c\uff0c\u88ab\u5220\u9664\u7684\u5143\u7d20\u4e0d\u4f1a\u4fdd\u7559\u4efb\u4f55\u5360\u4f4d\uff0c\u56e0\u6b64\u5982\u679c\u540e\u7eed\u9700\u8981\u663e\u793a\u8be5\u4e0b\u62c9\u6846\uff0c\u9700\u8981\u91cd\u65b0\u5c06\u5176\u6dfb\u52a0\u5230DOM\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function removeDropdown() {<\/p>\n<p>    var element = document.getElementById(&quot;myDropdown&quot;);<\/p>\n<p>    element.remove();<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5b8c\u5168\u4e0d\u9700\u8981\u5143\u7d20\u65f6\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u4e00\u65e6\u5220\u9664\uff0c\u8981\u518d\u6b21\u663e\u793a\u8be5\u5143\u7d20\u9700\u8981\u8f83\u591a\u7684\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5229\u7528CLASS\u52a8\u6001\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u52a8\u6001\u6dfb\u52a0\u548c\u79fb\u9664CSS\u7c7b\u6765\u63a7\u5236\u4e0b\u62c9\u6846\u7684\u663e\u793a\u548c\u9690\u85cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5c06\u6837\u5f0f\u548c\u884c\u4e3a\u5206\u79bb\uff0c\u66f4\u5bb9\u6613\u7ba1\u7406\u548c\u590d\u7528CSS\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728CSS\u4e2d\u5b9a\u4e49\u4e24\u4e2a\u7c7b\uff0c\u4e00\u4e2a\u7528\u4e8e\u9690\u85cf\uff0c\u4e00\u4e2a\u7528\u4e8e\u663e\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">.hidden {<\/p>\n<p>    display: none;<\/p>\n<p>}<\/p>\n<p>.visible {<\/p>\n<p>    display: block;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528JavaScript\u52a8\u6001\u5730\u7ed9\u4e0b\u62c9\u6846\u6dfb\u52a0\u6216\u79fb\u9664\u8fd9\u4e9b\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function hideDropdown() {<\/p>\n<p>    document.getElementById(&quot;myDropdown&quot;).classList.add(&quot;hidden&quot;);<\/p>\n<p>}<\/p>\n<p>function showDropdown() {<\/p>\n<p>    document.getElementById(&quot;myDropdown&quot;).classList.remove(&quot;hidden&quot;);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5229\u7528class\u52a8\u6001\u63a7\u5236<\/strong>\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\uff0c\u80fd\u591f\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u4e0b\u62c9\u6846\u7684\u663e\u793a\u72b6\u6001\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7684CSS\u7c7b\u6765\u5b9e\u73b0\u8fc7\u6e21\u6548\u679c\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u9898\u4e00\uff1a\u5982\u4f55\u7528JavaScript\u5b9e\u73b0\u4e0b\u62c9\u6846\u7684\u9690\u85cf\u548c\u663e\u793a\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8981\u4f7f\u7528JavaScript\u6765\u9690\u85cf\u4e0b\u62c9\u6846\uff0c\u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5c\u4e0b\u62c9\u6846\u5143\u7d20\u7684\u6837\u5f0f\uff08display\u5c5e\u6027\uff09\u6765\u63a7\u5236\u5176\u663e\u793a\u4e0e\u9690\u85cf\u3002\u53ef\u4ee5\u4f7f\u7528getElementById()\u65b9\u6cd5\u6765\u83b7\u53d6\u4e0b\u62c9\u6846\u7684DOM\u5bf9\u8c61\uff0c\u518d\u4f7f\u7528style\u5c5e\u6027\u6765\u6539\u53d8\u5b83\u7684display\u5c5e\u6027\u3002\u5f53\u9700\u8981\u9690\u85cf\u4e0b\u62c9\u6846\u65f6\uff0c\u5c06display\u5c5e\u6027\u8bbe\u7f6e\u4e3a&quot;none&quot;\uff1b\u5f53\u9700\u8981\u663e\u793a\u4e0b\u62c9\u6846\u65f6\uff0c\u5c06display\u5c5e\u6027\u8bbe\u7f6e\u4e3a&quot;block&quot;\u6216&quot;inline&quot;\u3002<\/p>\n<p><strong>\u95ee\u9898\u4e8c\uff1a\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5728JavaScript\u4e2d\u9690\u85cf\u4e0b\u62c9\u6846\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u9664\u4e86\u6539\u53d8\u4e0b\u62c9\u6846\u7684display\u5c5e\u6027\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528JavaScript\u4e2d\u7684classList\u5c5e\u6027\u6765\u6dfb\u52a0\u6216\u79fb\u9664\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7c7b\u540d\uff0c\u4ece\u800c\u4fee\u6539\u4e0b\u62c9\u6846\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a&quot;hidden&quot;\u7684\u7c7b\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u4e0b\u62c9\u6846\u7684classList\u4e2d\uff0c\u5f53\u9700\u8981\u9690\u85cf\u4e0b\u62c9\u6846\u65f6\uff0c\u6dfb\u52a0&quot;hidden&quot;\u7c7b\uff1b\u5f53\u9700\u8981\u663e\u793a\u4e0b\u62c9\u6846\u65f6\uff0c\u79fb\u9664&quot;hidden&quot;\u7c7b\u3002<\/p>\n<p><strong>\u95ee\u9898\u4e09\uff1a\u5982\u4f55\u901a\u8fc7JavaScript\u5b9e\u73b0\u52a8\u6001\u9690\u85cf\u548c\u663e\u793a\u4e0b\u62c9\u6846\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u53ef\u4ee5\u4f7f\u7528JavaScript\u4e2d\u7684\u4e8b\u4ef6\u76d1\u542c\u5668\u6216\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u52a8\u6001\u9690\u85cf\u548c\u663e\u793a\u4e0b\u62c9\u6846\u3002\u4f8b\u5982\uff0c\u5f53\u9f20\u6807\u79fb\u52a8\u5230\u67d0\u4e2a\u533a\u57df\u65f6\uff0c\u4f7f\u7528\u9f20\u6807\u79fb\u5165\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u663e\u793a\u4e0b\u62c9\u6846\uff1b\u5f53\u9f20\u6807\u79bb\u5f00\u8be5\u533a\u57df\u65f6\uff0c\u4f7f\u7528\u9f20\u6807\u79fb\u51fa\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u9690\u85cf\u4e0b\u62c9\u6846\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u4e00\u4e9b\u6761\u4ef6\u6765\u63a7\u5236\u4e0b\u62c9\u6846\u7684\u663e\u793a\u4e0e\u9690\u85cf\uff0c\u6bd4\u5982\u6839\u636e\u7528\u6237\u7684\u64cd\u4f5c\u6216\u9875\u9762\u7684\u72b6\u6001\u6765\u51b3\u5b9a\u662f\u5426\u663e\u793a\u4e0b\u62c9\u6846\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u5b9e\u73b0\u9690\u85cf\u4e0b\u62c9\u6846\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u8bbe\u7f6edisplay\u5c5e\u6027\u3001\u901a\u8fc7visibility\u5c5e\u6027\u3001\u4f7f\u7528r [&hellip;]","protected":false},"author":3,"featured_media":169262,"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\/169256"}],"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=169256"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169256\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169262"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=169256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=169256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=169256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}