{"id":172291,"date":"2024-05-08T17:47:29","date_gmt":"2024-05-08T09:47:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/172291.html"},"modified":"2024-05-08T17:47:33","modified_gmt":"2024-05-08T09:47:33","slug":"%e5%9b%becss%e5%a6%82%e4%bd%95%e6%8e%a7%e5%88%b6javascript%e8%b0%83%e7%94%a8%e7%9a%84%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/172291.html","title":{"rendered":"[\u56fe]CSS\u5982\u4f55\u63a7\u5236JavaScript\u8c03\u7528\u7684\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27043848\/5c2ce3b6-6d98-4481-bb5e-4f172f7d6358.webp\" alt=\"[\u56fe]CSS\u5982\u4f55\u63a7\u5236JavaScript\u8c03\u7528\u7684\u5185\u5bb9\" \/><\/p>\n<p><p>CSS\u63a7\u5236JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u7684\u65b9\u5f0f\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<strong>\u901a\u8fc7\u9009\u62e9\u5668\u6307\u5b9a\u6837\u5f0f\u3001\u4f7f\u7528\u5185\u8054\u6837\u5f0f\u3001\u64cd\u4f5c\u7c7b\u540d\u3001\u5229\u7528CSS\u53d8\u91cf\u3001\u4f7f\u7528!important\u89c4\u5219\u3001\u91c7\u7528JavaScript\u64cd\u4f5c\u5185\u8054\u6837\u5f0f\u5c5e\u6027<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u64cd\u4f5c\u7c7b\u540d\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u6d89\u53ca\u5c06\u9884\u5b9a\u4e49\u597d\u7684CSS\u7c7b\u901a\u8fc7JavaScript\u52a8\u6001\u6dfb\u52a0\u6216\u5220\u9664\uff0c\u4ee5\u5b9e\u73b0\u5bf9\u5143\u7d20\u6837\u5f0f\u7684\u7cbe\u786e\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7\u9009\u62e9\u5668\u6307\u5b9a\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7CSS\u9009\u62e9\u5668\u4e3a\u9875\u9762\u5143\u7d20\u5b9a\u5236\u6837\u5f0f\u662fCSS\u7684\u57fa\u7840\u529f\u80fd\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u7528\u6765\u63a7\u5236JavaScript\u52a8\u6001\u751f\u6210\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u4e3a\u7279\u5b9a\u7684ID\u3001\u7c7b\u6216\u5c5e\u6027\u521b\u5efa\u6837\u5f0f\u89c4\u5219\uff0c\u5f53JavaScript\u5411DOM\u4e2d\u6dfb\u52a0\u7b26\u5408\u8fd9\u4e9b\u9009\u62e9\u5668\u7684\u5143\u7d20\u65f6\uff0c\u76f8\u5e94\u7684\u6837\u5f0f\u4f1a\u88ab\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c<\/strong> CSS\u9009\u62e9\u5668\u80fd\u591f\u5339\u914d\u4efb\u4f55\u5728DOM\u4e2d\u7684\u5143\u7d20\uff0c\u5305\u62ecJavaScript\u52a8\u6001\u521b\u5efa\u6216\u4fee\u6539\u7684\u5143\u7d20\u3002\u901a\u8fc7ID\u3001\u7c7b\u6216\u7279\u5b9a\u5c5e\u6027\u4e3a\u5143\u7d20\u5b9a\u4e49\u6837\u5f0f\u540e\uff0c\u65e0\u8bba\u4f55\u65f6\u53ea\u8981\u5143\u7d20\u51fa\u73b0\u5728DOM\u4e2d\uff0c\u90fd\u5c06\u5e94\u7528\u8fd9\u4e9b\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u5176\u6b21\uff0c<\/strong> \u4e3a\u4e86\u4f7fCSS\u66f4\u5177\u6709\u53ef\u7ef4\u62a4\u6027\uff0c\u63a8\u8350\u4f7f\u7528\u7c7b\u9009\u62e9\u5668\u800c\u4e0d\u662fID\u9009\u62e9\u5668\uff0c\u56e0\u4e3a\u7c7b\u9009\u62e9\u5668\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u4e14\u4e0d\u5fc5\u662f\u552f\u4e00\u7684\uff0c\u8fd9\u5728JavaScript\u4e2d\u52a8\u6001\u6dfb\u52a0\u591a\u4e2a\u5177\u6709\u76f8\u540c\u6837\u5f0f\u7684\u5143\u7d20\u65f6\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u8054\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u5185\u8054\u6837\u5f0f\u5141\u8bb8\u76f4\u63a5\u5728HTML\u5143\u7d20\u4e0a\u58f0\u660e\u6837\u5f0f\u3002\u5f53\u4f7f\u7528JavaScript\u521b\u5efa\u6216\u4fee\u6539DOM\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5143\u7d20\u4e0a\u8bbe\u7f6e\u5185\u8054\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c<\/strong> \u5229\u7528JavaScript\u7684<code>element.style<\/code>\u5c5e\u6027\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5143\u7d20\u7684\u6837\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7acb\u5373\u770b\u5230\u6548\u679c\uff0c\u4e14\u4f18\u5148\u7ea7\u9ad8\u4e8e\u5916\u90e8\u548c\u5185\u90e8CSS\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><p><strong>\u5176\u6b21\uff0c<\/strong> \u5185\u8054\u6837\u5f0f\u975e\u5e38\u9002\u5408\u5355\u6b21\u4f7f\u7528\u7684\u6837\u5f0f\u66f4\u6539\uff0c\u4f46\u4e0d\u5229\u4e8e\u6837\u5f0f\u7684\u590d\u7528\u3002\u5728\u5904\u7406\u5927\u91cf\u6837\u5f0f\u6216\u9700\u8981\u9891\u7e41\u53d8\u66f4\u6837\u5f0f\u7684\u573a\u666f\u4e2d\uff0c\u901a\u8fc7\u4fee\u6539\u7c7b\u540d\u6216\u8005CSS\u89c4\u5219\u6765\u63a7\u5236\u6837\u5f0f\u53ef\u80fd\u4f1a\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u64cd\u4f5c\u7c7b\u540d<\/h3>\n<\/p>\n<p><p>JavaScript\u5141\u8bb8\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u79fb\u9664\u5143\u7d20\u7684\u7c7b\u540d\uff0c\u8fd9\u79cd\u65b9\u5f0f\u7ed3\u5408CSS\u9884\u5b9a\u4e49\u7684\u7c7b\u6837\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u63a7\u5236\u9875\u9762\u5143\u7d20\u7684\u5916\u89c2\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c<\/strong> \u901a\u8fc7<code>element.classList.add(&#039;className&#039;)<\/code>\u548c<code>element.classList.remove(&#039;className&#039;)<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u6216\u79fb\u9664\u7c7b\u540d\uff0c\u53ef\u4ee5\u4f7f\u5143\u7d20\u6309\u7167CSS\u5b9a\u4e49\u7684\u89c4\u5219\u8fdb\u884c\u6837\u5f0f\u53d8\u66f4\u3002<\/p>\n<\/p>\n<p><p><strong>\u5176\u6b21\uff0c<\/strong> \u7c7b\u540d\u7684\u64cd\u4f5c\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u4e1a\u52a1\u903b\u8f91\u6216\u7528\u6237\u4ea4\u4e92\u6765\u5207\u6362\u5143\u7d20\u72b6\u6001\u3002<strong>\u4f8b\u5982\uff0c<\/strong> \u5728\u7528\u6237\u70b9\u51fb\u4e00\u4e2a\u6309\u94ae\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a<code>.active<\/code>\u7c7b\u6765\u66f4\u6539\u6309\u94ae\u7684\u6837\u5f0f\uff0c\u8868\u793a\u5176\u4e3a\u6d3b\u52a8\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5229\u7528CSS\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>CSS\u53d8\u91cf\uff08\u4e5f\u79f0\u4f5cCSS\u81ea\u5b9a\u4e49\u5c5e\u6027\uff09\u4f7f\u5f97\u5728\u8fd0\u884c\u65f6\u901a\u8fc7JavaScript\u52a8\u6001\u63a7\u5236\u6837\u5f0f\u6210\u4e3a\u53ef\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c<\/strong> \u901a\u8fc7<code>:root<\/code>\u9009\u62e9\u5668\u5b9a\u4e49\u5168\u5c40CSS\u53d8\u91cf\u7136\u540e\u5728\u9700\u8981\u7684\u5730\u65b9\u5f15\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7JavaScript\u6765\u6539\u53d8\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\uff0c\u9875\u9762\u4e0a\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u7684\u6240\u6709\u6837\u5f0f\u90fd\u4f1a\u76f8\u5e94\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><p><strong>\u5176\u6b21\uff0c<\/strong> CSS\u53d8\u91cf\u7684\u53e6\u4e00\u4e2a\u4f18\u52bf\u662f\u5b83\u4eec\u53ef\u4ee5\u8de8\u8d8a\u5a92\u4f53\u67e5\u8be2\u4ee5\u53ca\u5d4c\u5957\u89c4\u5219\uff0c\u4f7f\u5f97\u7075\u6d3b\u8c03\u6574\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u6216\u7ec4\u4ef6\u5185\u90e8\u6837\u5f0f\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528!important\u89c4\u5219<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u901a\u5e38\u4e0d\u63a8\u8350\u9891\u7e41\u4f7f\u7528<code>!important<\/code>\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ef\u4ee5\u5f3a\u5236\u6837\u5f0f\u4f18\u5148\u7ea7\uff0c\u8986\u76d6\u73b0\u6709\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c<\/strong> \u5f53\u9700\u8981\u786e\u4fdd\u67d0\u4e2a\u6837\u5f0f\u4e0d\u53d7\u5176\u4ed6CSS\u89c4\u5219\u5f71\u54cd\u65f6\uff0c\u53ef\u4ee5\u5728CSS\u58f0\u660e\u4e2d\u6dfb\u52a0<code>!important<\/code>\u6765\u63d0\u5347\u8be5\u58f0\u660e\u7684\u4f18\u5148\u7ea7\u3002<\/p>\n<\/p>\n<p><p><strong>\u5176\u6b21\uff0c<\/strong> \u5728\u4e0e\u7b2c\u4e09\u65b9\u5185\u5bb9\u6216\u590d\u6742\u7684\u6837\u5f0f\u8868\u534f\u4f5c\u65f6\uff0c<code>!important<\/code>\u53ef\u4ee5\u4f5c\u4e3a\u4e34\u65f6\u4fee\u590d\u624b\u6bb5\uff0c\u5feb\u901f\u8986\u76d6\u53ef\u80fd\u5b58\u5728\u7684\u6837\u5f0f\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u91c7\u7528JavaScript\u64cd\u4f5c\u5185\u8054\u6837\u5f0f\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u901a\u8fc7JavaScript\u64cd\u4f5cDOM\u5143\u7d20\u7684\u5185\u8054\u6837\u5f0f\u5c5e\u6027\uff0c\u53ef\u4ee5\u7ec6\u7c92\u5ea6\u5730\u63a7\u5236\u5143\u7d20\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c<\/strong> JavaScript\u63d0\u4f9b\u4e86<code>element.style.property<\/code>\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u9488\u5bf9\u5355\u4e2a\u6837\u5f0f\u5c5e\u6027\u8fdb\u884c\u8bbe\u7f6e\u6216\u4fee\u6539\u3002\u8fd9\u5bf9\u4e8e\u5b9e\u73b0\u590d\u6742\u7684\u52a8\u6001\u6548\u679c\u6216\u52a8\u753b\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u5176\u6b21\uff0c<\/strong> \u64cd\u4f5cDOM\u5143\u7d20\u7684\u5185\u8054\u6837\u5f0f\u5c5e\u6027\u867d\u7136\u53ef\u4ee5\u5feb\u901f\u54cd\u5e94\u6837\u5f0f\u53d8\u5316\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\uff0c\u56e0\u6b64\u5efa\u8bae\u5728\u4f7f\u7528\u65f6\u5c3d\u91cf\u7ed3\u5408\u7c7b\u7684\u65b9\u5f0f\uff0c\u4fdd\u6301CSS\u7684\u7ec4\u7ec7\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: CSS\u5bf9\u4e8eJavaScript\u8c03\u7528\u7684\u5185\u5bb9\u6709\u4ec0\u4e48\u63a7\u5236\u80fd\u529b\uff1f<\/strong><br \/>\nA: CSS\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u9009\u62e9\u5668\u548c\u6837\u5f0f\u89c4\u5219\u6765\u63a7\u5236JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u7684\u5916\u89c2\u548c\u5e03\u5c40\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528CSS\u9009\u62e9\u5668\u6765\u9009\u4e2d\u7279\u5b9a\u7684HTML\u5143\u7d20\uff0c\u5e76\u901a\u8fc7\u5e94\u7528CSS\u6837\u5f0f\u89c4\u5219\u6765\u6539\u53d8\u5b83\u4eec\u7684\u6837\u5f0f\uff0c\u5982\u989c\u8272\u3001\u5b57\u4f53\u3001\u80cc\u666f\u7b49\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u4f7f\u7528CSS\u6765\u63a7\u5236\u901a\u8fc7JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u7684\u663e\u793a\u72b6\u6001\uff1f<\/strong><br \/>\nA: \u901a\u8fc7CSS\u7684display\u5c5e\u6027\u548cvisibility\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u7684\u663e\u793a\u72b6\u6001\u3002display\u5c5e\u6027\u53ef\u4ee5\u8bbe\u7f6e\u5143\u7d20\u7684\u663e\u793a\u65b9\u5f0f\uff0c\u5982block\u3001inline\u3001none\u7b49\uff0c\u4ece\u800c\u51b3\u5b9a\u5143\u7d20\u662f\u5426\u5728\u9875\u9762\u4e0a\u663e\u793a\u3002\u800cvisibility\u5c5e\u6027\u53ef\u4ee5\u8bbe\u7f6e\u5143\u7d20\u7684\u53ef\u89c1\u6027\uff0c\u5982visible\u548chidden\uff0c\u4ece\u800c\u51b3\u5b9a\u5143\u7d20\u662f\u5426\u53ef\u89c1\u3002\u8fd9\u4e9b\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7JavaScript\u6765\u52a8\u6001\u4fee\u6539\uff0c\u4ece\u800c\u5b9e\u73b0\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u63a7\u5236\u5185\u5bb9\u7684\u663e\u793a\u4e0e\u9690\u85cf\u3002<\/p>\n<p><strong>Q: CSS\u80fd\u5426\u63a7\u5236\u901a\u8fc7JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u7684\u54cd\u5e94\u5f0f\u8bbe\u8ba1\uff1f<\/strong><br \/>\nA: \u662f\u7684\uff0cCSS\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u901a\u8fc7JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u7684\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u3002\u901a\u8fc7\u4f7f\u7528CSS\u7684\u5a92\u4f53\u67e5\u8be2\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u8bbe\u5907\u5c3a\u5bf8\u548c\u5c4f\u5e55\u5206\u8fa8\u7387\uff0c\u4e3a\u4e0d\u540c\u7684\u8bbe\u5907\u63d0\u4f9b\u4e0d\u540c\u7684\u6837\u5f0f\u548c\u5e03\u5c40\u3002\u8fd9\u610f\u5473\u7740\u901a\u8fc7JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u53ef\u4ee5\u6839\u636e\u7528\u6237\u6240\u4f7f\u7528\u7684\u8bbe\u5907\u8fdb\u884c\u4f18\u5316\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u5c4f\u5e55\u4e0a\u5448\u73b0\u6700\u4f73\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"CSS\u63a7\u5236JavaScript\u8c03\u7528\u7684\u5185\u5bb9\u7684\u65b9\u5f0f\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a\u901a\u8fc7\u9009\u62e9\u5668\u6307\u5b9a\u6837\u5f0f\u3001\u4f7f\u7528\u5185\u8054\u6837\u5f0f\u3001\u64cd\u4f5c\u7c7b\u540d\u3001\u5229\u7528C [&hellip;]","protected":false},"author":3,"featured_media":172297,"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\/172291"}],"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=172291"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/172291\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/172297"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=172291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=172291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=172291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}