{"id":3248,"date":"2024-11-10T13:37:42","date_gmt":"2024-11-10T05:37:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3248\/"},"modified":"2024-11-10T13:37:42","modified_gmt":"2024-11-10T05:37:42","slug":"js%e5%a6%82%e4%bd%95%e6%94%b9%e5%8f%98css","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3248\/","title":{"rendered":"js\u5982\u4f55\u6539\u53d8css"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u6539\u53d8 css \u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 document.stylesheets \u5c5e\u6027\u4fee\u6539\u6837\u5f0f\u8868\u89c4\u5219\u3002\u76f4\u63a5\u901a\u8fc7\u5143\u7d20\u7684 style \u5c5e\u6027\u8bbe\u7f6e\u6216\u4fee\u6539 css \u6837\u5f0f\u3002\u4f7f\u7528 cssom api \u8bbf\u95ee\u548c\u4fee\u6539 css \u6837\u5f0f\u89c4\u5219\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808151545090.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u6539\u53d8css\u63d2\u56fe\" alt=\"js\u5982\u4f55\u6539\u53d8css\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u6539\u53d8 CSS<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>JavaScript \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u7528\u4e8e\u52a8\u6001\u4fee\u6539\u7f51\u9875\u5185\u5bb9\u548c\u6837\u5f0f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 JavaScript \u4e2d\u6539\u53d8 CSS\u3002<\/p>\n<p><strong>\u4f7f\u7528 document.styleSheets<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>document.styleSheets \u5c5e\u6027\u8fd4\u56de\u5f53\u524d\u6587\u6863\u7684\u6240\u6709\u6837\u5f0f\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u6539\u53d8\u6837\u5f0f\uff1a<\/p>\n<pre>\/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a\u6837\u5f0f\u8868\nconst sheet = document.styleSheets[0];\n\n\/\/ \u6dfb\u52a0\u4e00\u6761\u65b0\u89c4\u5219\nsheet.addRule('body', 'background-color: red;');\n\n\/\/ \u4fee\u6539\u73b0\u6709\u89c4\u5219\nsheet.rules[0].style.color = 'blue';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 style \u5c5e\u6027<\/strong><\/p>\n<p>\u6bcf\u4e2a HTML \u5143\u7d20\u90fd\u6709\u4e00\u4e2a style \u5c5e\u6027\uff0c\u5b83\u662f\u4e00\u4e2a CSS \u5c5e\u6027\u7684\u96c6\u5408\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u6539\u53d8\u6837\u5f0f\uff1a<\/p>\n<pre>\/\/ \u83b7\u53d6 body \u5143\u7d20\nconst body = document.querySelector('body');\n\n\/\/ \u8bbe\u7f6e\u80cc\u666f\u8272\nbody.style.backgroundColor = 'red';\n\n\/\/ \u8bbe\u7f6e\u5b57\u4f53\u989c\u8272\nbody.style.color = 'blue';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 CSSOM API<\/strong><\/p>\n<p>CSSOM API \u63d0\u4f9b\u4e86\u4e00\u7ec4\u65b9\u6cd5\u6765\u8bbf\u95ee\u548c\u4fee\u6539 CSS \u6837\u5f0f\u89c4\u5219\u3002\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u53ef\u4ee5\u6539\u53d8\u6837\u5f0f\uff1a<\/p>\n<pre>document.getElementById('myElement').classList.add('active');\ndocument.getElementById('myElement').style.setProperty('color', 'red');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e9b\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e inline \u6837\u5f0f\u6216\u4e0e HTML \u5143\u7d20\u5173\u8054\u7684\u6837\u5f0f\u8868\u3002<\/li>\n<li>\u5bf9\u4e8e\u5916\u90e8\u6837\u5f0f\u8868\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5f02\u6b65\u8bf7\u6c42\u6216\u5176\u4ed6\u6280\u672f\u6765\u52a8\u6001\u52a0\u8f7d\u548c\u4fee\u6539\u5b83\u4eec\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u6539\u53d8\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\u6539\u53d8 css \u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 document.stylesheets \u5c5e\u6027\u4fee\u6539\u6837\u5f0f\u8868\u89c4\u5219\u3002\u76f4\u63a5\u901a\u8fc7\u5143\u7d20\u7684 style \u5c5e\u6027\u8bbe\u7f6e\u6216\u4fee\u6539 css \u6837\u5f0f\u3002\u4f7f\u7528 cssom api \u8bbf\u95ee\u548c\u4fee\u6539 css \u6837\u5f0f\u89c4\u5219\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u6539\u53d8 CSS \u524d\u8a00 JavaScript \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u7528\u4e8e\u52a8\u6001\u4fee\u6539\u7f51\u9875\u5185\u5bb9\u548c\u6837\u5f0f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 JavaScript \u4e2d\u6539\u53d8 CSS\u3002 \u4f7f\u7528 document.styleSheets \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b document.styleSheets \u5c5e\u6027\u8fd4\u56de\u5f53\u524d\u6587\u6863\u7684\u6240\u6709\u6837\u5f0f\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u6539\u53d8\u6837\u5f0f\uff1a \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a\u6837\u5f0f\u8868 const sheet = document.styleSheets[0]; \/\/ \u6dfb\u52a0\u4e00\u6761\u65b0\u89c4\u5219 sheet.addRule(&#8216;body&#8217;, &#8216;background-color: red;&#8217;); \/\/ \u4fee\u6539\u73b0\u6709\u89c4\u5219 sheet.rules[0].style.color = &#8216;blue&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 style \u5c5e\u6027 \u6bcf\u4e2a HTML \u5143\u7d20\u90fd\u6709\u4e00\u4e2a style [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3248","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3248"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3248\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}