
将引入的 svg 转换为代码形式
在网页中引入 SVG 文件时,查看源代码可能只看到一个类似 ./test.svg 的文件路径,而其他网站引入的 SVG 却是大段的编码。本文将介绍如何通过代码的形式引入 SVG 文件。
为此,可以使用以下步骤:
以下代码示例演示了如何实现此过程:
<div id="test"></div>
fetch('https://static.segmentfault.com/main_site_next/614d2165/_next/static/media/sf-icon-small.4d244289.svg')
.then(body => body.text())
.then(svg => (new DOMParser).parseFromString(svg, 'image/svg+xml'))
.then(actualSVG => {
// append to the body or do your thing
document.querySelector('#test').appendChild(actualSVG.documentElement)
});以上就是如何将 SVG 文件引入网页并显示其内容?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号