{"id":292588,"date":"2024-05-20T14:22:22","date_gmt":"2024-05-20T06:22:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/292588.html"},"modified":"2024-05-20T14:22:31","modified_gmt":"2024-05-20T06:22:31","slug":"js-%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5%e5%a4%96%e9%83%a8-css","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/292588.html","title":{"rendered":"js \u5982\u4f55\u5f15\u5165\u5916\u90e8 css"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27135258\/40b3024f-7d8f-4cb4-9828-6c31de5e923f.webp\" alt=\"js \u5982\u4f55\u5f15\u5165\u5916\u90e8 css\" \/><\/p>\n<p><p>JavaScript\u5f15\u5165\u5916\u90e8CSS\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec<strong>\u52a8\u6001\u521b\u5efa\u6807\u7b7e\u3001\u4f7f\u7528<code>import<\/code>\u6307\u4ee4\u548c\u5229\u7528<code>document.write()<\/code><\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u4f46\u5176\u4e2d<strong>\u52a8\u6001\u521b\u5efa\u6807\u7b7e<\/strong>\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u9700\u8981\u6839\u636e\u4e0d\u540c\u60c5\u51b5\u52a8\u6001\u6539\u53d8\u6837\u5f0f\u6216\u6309\u9700\u52a0\u8f7dCSS\u7684\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u52a8\u6001\u521b\u5efa<code>&lt;link&gt;<\/code>\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u52a8\u6001\u521b\u5efa<code>&lt;link&gt;<\/code>\u6807\u7b7e\u662f\u4e00\u79cd\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u5f15\u5165\u5916\u90e8CSS\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7JavaScript\u76f4\u63a5\u64cd\u4f5cDOM\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684<code>link<\/code>\u5143\u7d20\uff0c\u5e76\u8bbe\u7f6e\u5176<code>rel<\/code>\u548c<code>href<\/code>\u5c5e\u6027\u6307\u5411\u5916\u90e8CSS\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u6700\u540e\u5c06\u8fd9\u4e2a<code>link<\/code>\u5143\u7d20\u6dfb\u52a0\u5230<code>document<\/code>\u7684<code>head<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa<code>&lt;link&gt;<\/code>\u5143\u7d20<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u901a\u8fc7JavaScript\u7684<code>document.createElement()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684<code>link<\/code>\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var link = document.createElement(&#039;link&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bbe\u7f6e<code>&lt;link&gt;<\/code>\u5143\u7d20\u5c5e\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u8bbe\u7f6e<code>link<\/code>\u5143\u7d20\u7684<code>rel<\/code>\u5c5e\u6027\u4e3a<code>stylesheet<\/code>\uff0c\u5c06\u5176\u5b9a\u4e49\u4e3a\u6837\u5f0f\u8868\uff0c\u5e76\u901a\u8fc7<code>href<\/code>\u5c5e\u6027\u6307\u5b9a\u5916\u90e8CSS\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">link.rel = &#039;stylesheet&#039;;<\/p>\n<p>link.href = &#039;https:\/\/example.com\/style.css&#039;;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5c06<code>&lt;link&gt;<\/code>\u5143\u7d20\u6dfb\u52a0\u5230<code>head<\/code>\u4e2d<\/strong><\/li>\n<\/ol>\n<p><p>\u6700\u540e\uff0c\u901a\u8fc7<code>document.getElementsByTagName(&#039;head&#039;)[0].appendChild(link);<\/code>\u6216<code>document.head.appendChild(link);<\/code>\u5c06\u8fd9\u4e2a<code>link<\/code>\u5143\u7d20\u6dfb\u52a0\u5230<code>document<\/code>\u7684<code>head<\/code>\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u5916\u90e8CSS\u7684\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.getElementsByTagName(&#039;head&#039;)[0].appendChild(link);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>@import<\/code>\u6307\u4ee4<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u901a\u8fc7<code>@import<\/code>\u6307\u4ee4\u540c\u6837\u53ef\u4ee5\u5f15\u5165\u5916\u90e8CSS\uff0c\u4f46\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u5728CSS\u6587\u4ef6\u5185\u4f7f\u7528\u66f4\u4e3a\u5e38\u89c1\u3002\u7136\u800c\uff0c\u501f\u52a9\u4e8eJavaScript\u52a8\u6001\u521b\u5efa\u7684\u6807\u7b7e\uff0c\u4e5f\u80fd\u5b9e\u73b0\u901a\u8fc7<code>@import<\/code>\u5f15\u5165\u5916\u90e8CSS\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u5e76\u6dfb\u52a0<code>&lt;style&gt;<\/code>\u5143\u7d20<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u52a8\u6001\u521b\u5efa\u4e00\u4e2a<code>&lt;style&gt;<\/code>\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u6587\u6863\u7684<code>head<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var style = document.createElement(&#039;style&#039;);<\/p>\n<p>document.head.appendChild(style);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528<code>@import<\/code>\u6307\u4ee4\u5f15\u5165CSS<\/strong><\/li>\n<\/ol>\n<p><p>\u7136\u540e\uff0c\u901a\u8fc7<code>style.sheet.insertRule()<\/code>\u65b9\u6cd5\uff0c\u5c06\u542b\u6709<code>@import<\/code>\u6307\u4ee4\u7684\u89c4\u5219\u6dfb\u52a0\u5230\u8fd9\u4e2a<code>&lt;style&gt;<\/code>\u5143\u7d20\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">style.sheet.insertRule(&quot;@import url(&#039;https:\/\/example.com\/style.css&#039;);&quot;, 0);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528<code>document.write()<\/code><\/h3>\n<\/p>\n<p><p><code>document.write()<\/code>\u662f\u4e00\u4e2a\u65e7\u5f0f\u4e14\u4e0d\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u5411\u6587\u6863\u6d41\u4e2d\u5199\u5165HTML\u5b57\u7b26\u4e32\uff0c\u5305\u62ec<code>&lt;link&gt;<\/code>\u6807\u7b7e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u76f4\u63a5\u5199\u5165<code>&lt;link&gt;<\/code>\u6807\u7b7e<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>document.write()<\/code>\u65b9\u6cd5\uff0c\u76f4\u63a5\u5c06<code>&lt;link&gt;<\/code>\u6807\u7b7e\u7684HTML\u5b57\u7b26\u4e32\u5199\u5165\u5230\u6587\u6863\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.write(&#039;&lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/example.com\/style.css&quot;&gt;&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6ce8\u610f<\/strong><\/li>\n<\/ol>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>document.write()<\/code>\u5982\u679c\u5728\u6587\u6863\u52a0\u8f7d\u5b8c\u6210\u540e\u4f7f\u7528\uff0c\u4f1a\u5bfc\u81f4\u6574\u4e2a\u9875\u9762\u7684\u5185\u5bb9\u88ab\u91cd\u5199\uff0c\u56e0\u6b64\u5b83\u7684\u4f7f\u7528\u573a\u666f\u6781\u5176\u6709\u9650\uff0c\u4e14\u4e0d\u63a8\u8350\u5728\u73b0\u4ee3Web\u5f00\u53d1\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3\uff1a<strong>\u867d\u7136\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0JavaScript\u5f15\u5165\u5916\u90e8CSS\uff0c\u4f46<\/strong>\u52a8\u6001\u521b\u5efa<code>&lt;link&gt;<\/code>\u6807\u7b7e<\/strong>\u56e0\u5176\u7075\u6d3b\u6027\u548c\u9ad8\u6548\u6027\uff0c\u6210\u4e3a\u4e86\u6700\u4e3a\u5e38\u89c1\u548c\u63a8\u8350\u7684\u5b9e\u8df5\u65b9\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u7075\u6d3b\u5730\u52a0\u8f7d\u548c\u66f4\u65b0\u9875\u9762\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728JavaScript\u4e2d\u5f15\u5165\u5916\u90e8CSS\u6587\u4ef6\uff1f<\/strong><\/p>\n<p>CSS\u6837\u5f0f\u8868\u662f\u7528\u4e8e\u5b9a\u4e49\u7f51\u9875\u5916\u89c2\u548c\u5e03\u5c40\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5728JavaScript\u4e2d\u5f15\u5165\u5916\u90e8CSS\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\uff0c\u5728HTML\u6587\u4ef6\u7684\u5143\u7d20\u5185\u90e8\uff0c\u6dfb\u52a0\u4e00\u4e2a\u6807\u7b7e\u7528\u4e8e\u5f15\u5165\u5916\u90e8CSS\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<br \/>\n<code>&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;styles.css&quot;&gt;<\/code><\/p>\n<\/li>\n<li>\n<p>\u5176\u6b21\uff0c\u5728JavaScript\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u6587\u6863\u7684\u5934\u90e8\u5143\u7d20\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5934\u90e8\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">var cssLink = document.createElement(&quot;link&quot;);\ncssLink.rel = &quot;stylesheet&quot;;\ncssLink.href = &quot;styles.css&quot;;\ndocument.head.appendChild(cssLink);\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6700\u540e\uff0c\u786e\u4fdd\u5c06CSS\u6587\u4ef6\uff08styles.css\uff09\u4e0eHTML\u6587\u4ef6\u548cJavaScript\u6587\u4ef6\u653e\u7f6e\u5728\u76f8\u540c\u7684\u76ee\u5f55\u4e2d\uff0c\u6216\u8005\u4f7f\u7528\u6b63\u786e\u7684\u76f8\u5bf9\u8def\u5f84\u5f15\u7528CSS\u6587\u4ef6\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>2. JavaScript\u5982\u4f55\u52a8\u6001\u6539\u53d8\u5916\u90e8CSS\u7684\u5f15\u7528\uff1f<\/strong><\/p>\n<p>\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728JavaScript\u4e2d\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6539\u53d8\u5f15\u7528\u7684CSS\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u79cd\u65b9\u6cd5\u6765\u52a8\u6001\u6539\u53d8\u5916\u90e8CSS\u6587\u4ef6\u7684\u5f15\u7528\uff1a<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\uff0c\u5728HTML\u6587\u4ef6\u7684\u5143\u7d20\u4e2d\uff0c\u4e3a\u5143\u7d20\u6307\u5b9a\u4e00\u4e2aid\u3002\u4f8b\u5982\uff1a<br \/>\n<code>&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;styles1.css&quot; id=&quot;myStylesheet&quot;&gt;<\/code><\/p>\n<\/li>\n<li>\n<p>\u5176\u6b21\uff0c\u5728JavaScript\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u5143\u7d20\u7684\u5f15\u7528\uff0c\u5e76\u4f7f\u7528setAttribute\u65b9\u6cd5\u4fee\u6539\u5176href\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">var stylesheet = document.getElementById(&quot;myStylesheet&quot;);\nstylesheet.setAttribute(&quot;href&quot;, &quot;styles2.css&quot;);\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6700\u540e\uff0c\u5f53JavaScript\u4ee3\u7801\u6267\u884c\u65f6\uff0cCSS\u6587\u4ef6\u7684\u5f15\u7528\u5c06\u66f4\u6539\u4e3a\u65b0\u6307\u5b9a\u7684\u6587\u4ef6\uff08styles2.css\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>3. JavaScript\u5982\u4f55\u52a8\u6001\u6dfb\u52a0\u5185\u8054\u6837\u5f0f\u800c\u4e0d\u4f7f\u7528\u5916\u90e8CSS\u6587\u4ef6\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u901a\u8fc7\u5916\u90e8CSS\u6587\u4ef6\u5f15\u5165\u6837\u5f0f\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5728JavaScript\u4e2d\u52a8\u6001\u6dfb\u52a0\u5185\u8054\u6837\u5f0f\u6765\u6539\u53d8\u5143\u7d20\u7684\u5916\u89c2\u3002\u4ee5\u4e0b\u662f\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\uff0c\u83b7\u53d6\u8981\u6dfb\u52a0\u5185\u8054\u6837\u5f0f\u7684\u5143\u7d20\u7684\u5f15\u7528\u6216\u521b\u5efa\u65b0\u7684\u5143\u7d20\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u6b21\uff0c\u5728JavaScript\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5143\u7d20\u7684style\u5c5e\u6027\u6765\u6307\u5b9a\u5185\u8054\u6837\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">var element = document.getElementById(&quot;myElement&quot;);\nelement.style.color = &quot;red&quot;;\nelement.style.backgroundColor = &quot;yellow&quot;;\nelement.style.fontSize = &quot;20px&quot;;\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6700\u540e\uff0c\u5f53JavaScript\u4ee3\u7801\u6267\u884c\u65f6\uff0c\u5143\u7d20\u7684\u6837\u5f0f\u5c06\u6839\u636e\u6307\u5b9a\u7684\u5c5e\u6027\u503c\u8fdb\u884c\u66f4\u6539\uff0c\u4ece\u800c\u6539\u53d8\u5143\u7d20\u7684\u5916\u89c2\u3002\u8bf7\u8bb0\u4f4f\uff0c\u4f7f\u7528\u5185\u8054\u6837\u5f0f\u65f6\u5e94\u8c28\u614e\uff0c\u5e76\u4e14\u5c3d\u91cf\u4fdd\u6301\u6837\u5f0f\u4e0e\u5185\u5bb9\u5206\u79bb\u7684\u539f\u5219\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"JavaScript\u5f15\u5165\u5916\u90e8CSS\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u52a8\u6001\u521b\u5efa\u6807\u7b7e\u3001\u4f7f\u7528import\u6307\u4ee4\u548c\u5229\u7528document.wr [&hellip;]","protected":false},"author":3,"featured_media":292596,"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\/292588"}],"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=292588"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/292588\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/292596"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=292588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=292588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=292588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}