{"id":25778,"date":"2024-11-24T09:35:50","date_gmt":"2024-11-24T01:35:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25778\/"},"modified":"2024-11-24T09:35:50","modified_gmt":"2024-11-24T01:35:50","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e7%bd%91%e9%a1%b5%e4%b8%ad%e5%bc%95%e5%85%a5%e7%9a%84-svg-%e6%96%87%e4%bb%b6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%bb%a3%e7%a0%81%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25778\/","title":{"rendered":"\u5982\u4f55\u5c06\u7f51\u9875\u4e2d\u5f15\u5165\u7684 SVG \u6587\u4ef6\u8f6c\u6362\u4e3a\u4ee3\u7801\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173070342379149.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u7f51\u9875\u4e2d\u5f15\u5165\u7684 SVG \u6587\u4ef6\u8f6c\u6362\u4e3a\u4ee3\u7801\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u7f51\u9875\u4e2d\u5f15\u5165\u7684 SVG \u6587\u4ef6\u8f6c\u6362\u4e3a\u4ee3\u7801\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5c06\u7f51\u9875\u4e2d\u5f15\u5165\u7684 svg \u8f6c\u6362\u4e3a\u4ee3\u7801<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728\u7f51\u9875\u4e2d\u5f15\u5165\u4e86\u4e00\u4e2a SVG \u6587\u4ef6\uff0c\u4f46\u662f\u6e90\u4ee3\u7801\u4e2d\u53ea\u663e\u793a\u4e00\u4e2a\u7b80\u5355\u7684 SVG \u6587\u4ef6\u8def\u5f84 (\u4f8b\u5982\uff0c.\/test.svg)\uff0c\u800c\u5176\u4ed6\u7f51\u7ad9\u7684 SVG \u5f15\u5165\u901a\u5e38\u662f\u4e00\u5927\u4e32\u7f16\u7801\u3002\u5982\u4f55\u5b9e\u73b0\u8fd9\u79cd\u7f16\u7801\u5f15\u5165\u65b9\u5f0f\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 fetch \u548c DOMParser API \u6765\u5b9e\u73b0\uff1a<\/p>\n<p><strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>\/\/ \u53d6\u4ee3\u4f60\u60f3\u83b7\u53d6SVG\u7684\u6587\u4ef6\u5730\u5740\nfetch('https:\/\/static.segmentfault.com\/main_site_next\/614d2165\/_next\/static\/media\/sf-icon-small.4d244289.svg')\n  .then(body =&gt; body.text())\n  .then(svg =&gt; (new DOMParser).parseFromString(svg, 'image\/svg+xml'))\n  .then(actualSVG =&gt; {\n    \/\/ \u5c06\u5b9e\u9645\u7684SVG\u6dfb\u52a0\u5230\u9875\u9762\u4e2d\n    document.querySelector('#test').appendChild(actualSVG.documentElement)\n  });<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528\u8bf4\u660e\uff1a<\/strong><\/p>\n<ol>\n<li>\u5c06\u4ee3\u7801\u4e2d\u7684 SVG \u6587\u4ef6 URL \u66ff\u6362\u4e3a\u4f60\u81ea\u5df1\u7684 URL\u3002<\/li>\n<li>\u5c06 #test \u66ff\u6362\u4e3a\u4f60\u5e0c\u671b\u63d2\u5165 SVG \u7684\u5143\u7d20 ID\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4ece\u7f51\u9875\u4e2d\u83b7\u53d6 SVG \u7684\u5b9e\u9645\u7f16\u7801\uff0c\u5e76\u5c06\u5176\u52a8\u6001\u6dfb\u52a0\u5230\u4f60\u7684\u4ee3\u7801\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u7f51\u9875\u4e2d\u5f15\u5165\u7684 SVG \u6587\u4ef6\u8f6c\u6362\u4e3a\u4ee3\u7801\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>\u5c06\u7f51\u9875\u4e2d\u5f15\u5165\u7684 svg \u8f6c\u6362\u4e3a\u4ee3\u7801 \u95ee\u9898\uff1a \u5728\u7f51\u9875\u4e2d\u5f15\u5165\u4e86\u4e00\u4e2a SVG \u6587\u4ef6\uff0c\u4f46\u662f\u6e90\u4ee3\u7801\u4e2d\u53ea\u663e\u793a\u4e00\u4e2a\u7b80\u5355\u7684 SVG \u6587\u4ef6\u8def\u5f84 (\u4f8b\u5982\uff0c.\/test.svg)\uff0c\u800c\u5176\u4ed6\u7f51\u7ad9\u7684 SVG \u5f15\u5165\u901a\u5e38\u662f\u4e00\u5927\u4e32\u7f16\u7801\u3002\u5982\u4f55\u5b9e\u73b0\u8fd9\u79cd\u7f16\u7801\u5f15\u5165\u65b9\u5f0f\uff1f \u7b54\u6848\uff1a \u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 fetch \u548c DOMParser API \u6765\u5b9e\u73b0\uff1a \u4ee3\u7801\uff1a \/\/ \u53d6\u4ee3\u4f60\u60f3\u83b7\u53d6SVG\u7684\u6587\u4ef6\u5730\u5740 fetch(&#8216;https:\/\/static.segmentfault.com\/main_site_next\/614d2165\/_next\/static\/media\/sf-icon-small.4d244289.svg&#8217;) .then(body =&gt; body.text()) .then(svg =&gt; (new DOMParser).parseFromString(svg, &#8216;image\/svg+xml&#8217;)) .then(actualSVG =&gt; { \/\/ \u5c06\u5b9e\u9645\u7684SVG\u6dfb\u52a0\u5230\u9875\u9762\u4e2d document.querySelector(&#8216;#test&#8217;).appendChild(actualSVG.documentElement) }); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u8bf4\u660e\uff1a \u5c06\u4ee3\u7801\u4e2d\u7684 SVG \u6587\u4ef6 URL \u66ff\u6362\u4e3a\u4f60\u81ea\u5df1\u7684 URL\u3002 \u5c06 #test \u66ff\u6362\u4e3a\u4f60\u5e0c\u671b\u63d2\u5165 SVG \u7684\u5143\u7d20 ID\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4ece\u7f51\u9875\u4e2d\u83b7\u53d6 [&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-25778","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25778","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=25778"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25778\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}