使用fonticon时 直接<svg class="icon" aria-hidden="true"> <use xlink:href="#icon-xxx"></use> </svg>可以显示图片
js动态添加svg标签无法显示图片
两个问题:
1.svg基于xml 在html中使用需要指明namespace
2.查阅mdn文档(:href)发现SVG 2 removed the need for the xlink
namespace, so instead of xlink:href
you should use href
.
// 创建svg use需要指明命名空间let icon = ateElementNS("","svg");icon.setAttribute("xmlns", "");icon.setAttribute("class", "icon");icon.setAttribute("aria-hidden", "true");icon.setAttribute("fill", "currentColor");//use元素let use = ateElementNS("","use");// xlink:href替换为href成功显示图标use.setAttribute("href", "#icon-xxx");icon.appendChild(use);
本文发布于:2024-01-31 14:13:08,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170668158829103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |