{"id":2269,"date":"2024-11-10T09:32:54","date_gmt":"2024-11-10T01:32:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2269\/"},"modified":"2024-11-10T09:32:54","modified_gmt":"2024-11-10T01:32:54","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e7%bd%91%e9%a1%b5%e5%bc%95%e5%85%a5%e7%9a%84-svg-%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%bc%96%e7%a0%81%e5%bd%a2%e5%bc%8f%e5%b1%95%e7%a4%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2269\/","title":{"rendered":"\u5982\u4f55\u5c06\u7f51\u9875\u5f15\u5165\u7684 SVG \u8f6c\u6362\u4e3a\u7f16\u7801\u5f62\u5f0f\u5c55\u793a\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173070353817662.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u7f51\u9875\u5f15\u5165\u7684 SVG \u8f6c\u6362\u4e3a\u7f16\u7801\u5f62\u5f0f\u5c55\u793a\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u7f51\u9875\u5f15\u5165\u7684 SVG \u8f6c\u6362\u4e3a\u7f16\u7801\u5f62\u5f0f\u5c55\u793a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u7f51\u9875\u5f15\u5165\u7684 svg \u53d8\u6210\u7f16\u7801\u5f62\u5f0f\uff1f<\/strong><\/p>\n<p>\u60a8\u5728\u7f51\u7ad9\u4e2d\u5f15\u5165\u7684 SVG \u6587\u4ef6\u4ee5 &#8220;.\/test.svg&#8221; \u7684\u5f62\u5f0f\u663e\u793a\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u800c\u60a8\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e32\u7f16\u7801\uff0c\u7c7b\u4f3c\u4e8e\u60a8\u5728\u5176\u4ed6\u7f51\u7ad9\u4e0a\u770b\u5230\u7684\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a DOM \u5143\u7d20\uff1a<\/strong><\/p>\n<pre>&lt;div id=\"test\"&gt;&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528 Fetch API \u68c0\u7d22 SVG \u6587\u4ef6\uff1a<\/strong><\/p>\n<pre>fetch('https:\/\/static.segmentfault.com\/main_site_next\/614d2165\/_next\/static\/media\/sf-icon-small.4d244289.svg')\n    .then(body =&gt; body.text())<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u8f6c\u6362\u6587\u672c\u4e3a SVG \u6587\u6863\u5bf9\u8c61\uff1a<\/strong><\/p>\n<pre>.then(svg =&gt; (new DOMParser).parseFromString(svg, 'image\/svg+xml'))<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u5c06 SVG \u6587\u6863\u5bf9\u8c61\u9644\u52a0\u5230 DOM \u5143\u7d20\uff1a<\/strong><\/p>\n<pre>.then(actualSVG =&gt; {\n    document.querySelector('#test').appendChild(actualSVG.documentElement)\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u4ece\u8fdc\u7a0b URL \u68c0\u7d22 SVG \u6587\u4ef6\u3001\u5c06\u5176\u8f6c\u6362\u4e3a SVG \u6587\u6863\u5bf9\u8c61\u5e76\u5c06\u5176\u9644\u52a0\u5230 HTML \u4e2d\u7684\u6307\u5b9a DOM \u5143\u7d20\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u7f51\u9875\u5f15\u5165\u7684 SVG \u8f6c\u6362\u4e3a\u7f16\u7801\u5f62\u5f0f\u5c55\u793a\uff1f\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>\u5982\u4f55\u5c06\u7f51\u9875\u5f15\u5165\u7684 svg \u53d8\u6210\u7f16\u7801\u5f62\u5f0f\uff1f \u60a8\u5728\u7f51\u7ad9\u4e2d\u5f15\u5165\u7684 SVG \u6587\u4ef6\u4ee5 &#8220;.\/test.svg&#8221; \u7684\u5f62\u5f0f\u663e\u793a\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u800c\u60a8\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e32\u7f16\u7801\uff0c\u7c7b\u4f3c\u4e8e\u60a8\u5728\u5176\u4ed6\u7f51\u7ad9\u4e0a\u770b\u5230\u7684\u3002 \u5b9e\u73b0\u65b9\u6cd5\uff1a 1. \u521b\u5efa\u4e00\u4e2a DOM \u5143\u7d20\uff1a &lt;div id=&#8221;test&#8221;&gt;&lt;\/div&gt; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Fetch API \u68c0\u7d22 SVG \u6587\u4ef6\uff1a fetch(&#8216;https:\/\/static.segmentfault.com\/main_site_next\/614d2165\/_next\/static\/media\/sf-icon-small.4d244289.svg&#8217;) .then(body =&gt; body.text()) \u767b\u5f55\u540e\u590d\u5236 3. \u8f6c\u6362\u6587\u672c\u4e3a SVG \u6587\u6863\u5bf9\u8c61\uff1a .then(svg =&gt; (new DOMParser).parseFromString(svg, &#8216;image\/svg+xml&#8217;)) \u767b\u5f55\u540e\u590d\u5236 4. \u5c06 SVG \u6587\u6863\u5bf9\u8c61\u9644\u52a0\u5230 DOM \u5143\u7d20\uff1a .then(actualSVG =&gt; { document.querySelector(&#8216;#test&#8217;).appendChild(actualSVG.documentElement) }); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u4ece\u8fdc\u7a0b URL \u68c0\u7d22 SVG \u6587\u4ef6\u3001\u5c06\u5176\u8f6c\u6362\u4e3a [&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-2269","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2269","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=2269"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2269\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}