{"id":52985,"date":"2024-12-03T13:46:55","date_gmt":"2024-12-03T05:46:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52985\/"},"modified":"2024-12-03T13:46:55","modified_gmt":"2024-12-03T05:46:55","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-svg-%e6%96%87%e4%bb%b6%e5%bc%95%e5%85%a5%e7%bd%91%e9%a1%b5%e5%b9%b6%e6%98%be%e7%a4%ba%e5%85%b6%e5%86%85%e5%ae%b9%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52985\/","title":{"rendered":"\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f <\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f \u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/17325115606744074845afa.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5c06\u5f15\u5165\u7684 SVG \u8f6c\u6362\u4e3a\u4ee3\u7801\u5f62\u5f0f<\/strong><\/p>\n<p>\u5728\u7f51\u9875\u4e2d\u5f15\u5165 SVG \u6587\u4ef6\u65f6\uff0c\u67e5\u770b\u6e90\u4ee3\u7801\u53ef\u80fd\u53ea\u770b\u5230\u4e00\u4e2a\u7c7b\u4f3c .\/test.svg \u7684\u6587\u4ef6\u8def\u5f84\uff0c\u800c\u5176\u4ed6\u7f51\u7ad9\u5f15\u5165\u7684 SVG \u5374\u662f\u5927\u6bb5\u7684\u7f16\u7801\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u4ee3\u7801\u7684\u5f62\u5f0f\u5f15\u5165 SVG \u6587\u4ef6\u3002<\/p>\n<p>\u4e3a\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u83b7\u53d6 SVG \u6587\u4ef6\u7684\u5185\u5bb9\uff1a<\/strong> \u4f7f\u7528 fetch API \u83b7\u53d6\u5f15\u5165 SVG \u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u89e3\u6790\u4e3a\u6587\u672c\u3002<\/li>\n<li><strong>\u521b\u5efa DOMParser\uff1a<\/strong> \u521b\u5efa\u4e00\u4e2a DOMParser \u5bf9\u8c61\uff0c\u7528\u4e8e\u5c06 SVG \u6587\u672c\u89e3\u6790\u4e3a\u6587\u6863\u5bf9\u8c61\u6a21\u578b (DOM)\u3002<\/li>\n<li><strong>\u89e3\u6790 SVG \u6587\u672c\uff1a<\/strong> \u4f7f\u7528 DOMParser \u7684 parseFromString \u65b9\u6cd5\u5c06 SVG \u6587\u672c\u89e3\u6790\u4e3a DOM\u3002<\/li>\n<li><strong>\u83b7\u53d6 SVG \u6839\u5143\u7d20\uff1a<\/strong> \u4ece\u89e3\u6790\u7684 DOM \u4e2d\u83b7\u53d6 SVG \u7684\u6839\u5143\u7d20\uff08\u901a\u5e38\u662f &lt;svg&gt; \u5143\u7d20\uff09\u3002<\/li>\n<li><strong>\u5c06 SVG \u5143\u7d20\u6dfb\u52a0\u5230\u9875\u9762\uff1a<\/strong> \u4f7f\u7528 DOM API \u5c06 SVG \u6839\u5143\u7d20\u6dfb\u52a0\u5230\u6307\u5b9a\u7684\u76ee\u6807\u5bb9\u5668\u4e2d\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u6b64\u8fc7\u7a0b\uff1a<\/p>\n<pre>&lt;div id=\"test\"&gt;&lt;\/div&gt;<\/pre>\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())\n    .then(svg =&gt; (new DOMParser).parseFromString(svg, 'image\/svg+xml'))\n    .then(actualSVG =&gt; {\n        \/\/ append to the body or do your thing\n        document.querySelector('#test').appendChild(actualSVG.documentElement)\n    });<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f \u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Win11\u600e\u4e48\u5c06\u684c\u9762\u56fe\u6807\u81ea\u7531\u6446\u653e Win11\u684c\u9762\u56fe\u6807\u81ea\u7531\u6446\u653e\u7684\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5982\u4f55\u5c06 SVG \u6587\u4ef6\u5f15\u5165\u7f51\u9875\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1f \u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 \u5c06\u5f15\u5165\u7684 SVG \u8f6c\u6362\u4e3a\u4ee3\u7801\u5f62\u5f0f \u5728\u7f51\u9875\u4e2d\u5f15\u5165 SVG \u6587\u4ef6\u65f6\uff0c\u67e5\u770b\u6e90\u4ee3\u7801\u53ef\u80fd\u53ea\u770b\u5230\u4e00\u4e2a\u7c7b\u4f3c .\/test.svg \u7684\u6587\u4ef6\u8def\u5f84\uff0c\u800c\u5176\u4ed6\u7f51\u7ad9\u5f15\u5165\u7684 SVG \u5374\u662f\u5927\u6bb5\u7684\u7f16\u7801\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u4ee3\u7801\u7684\u5f62\u5f0f\u5f15\u5165 SVG \u6587\u4ef6\u3002 \u4e3a\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u83b7\u53d6 SVG \u6587\u4ef6\u7684\u5185\u5bb9\uff1a \u4f7f\u7528 fetch API \u83b7\u53d6\u5f15\u5165 SVG \u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u89e3\u6790\u4e3a\u6587\u672c\u3002 \u521b\u5efa DOMParser\uff1a \u521b\u5efa\u4e00\u4e2a DOMParser \u5bf9\u8c61\uff0c\u7528\u4e8e\u5c06 SVG \u6587\u672c\u89e3\u6790\u4e3a\u6587\u6863\u5bf9\u8c61\u6a21\u578b (DOM)\u3002 \u89e3\u6790 SVG \u6587\u672c\uff1a \u4f7f\u7528 DOMParser \u7684 parseFromString \u65b9\u6cd5\u5c06 SVG \u6587\u672c\u89e3\u6790\u4e3a DOM\u3002 \u83b7\u53d6 SVG \u6839\u5143\u7d20\uff1a \u4ece\u89e3\u6790\u7684 DOM \u4e2d\u83b7\u53d6 SVG [&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-52985","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52985","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=52985"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52985\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}