js动态创建svg与use 使用iconfont symbol

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./iconfont/iconfont.css">
    
</head>
<body>

 <!-- <svg class="icon svg-icon" aria-hidden="true">
                  <use xlink:href="#icon1"></use>
                </svg> -->
    <script src="./iconfont/iconfont.js"></script>
    <script>
        let svgns = "http://www.w3.org/2000/svg";
      let xlinkns = "http://www.w3.org/1999/xlink";
        let icon = document.createElementNS(svgns, "svg");
        icon.setAttribute("aria-hidden", true);
        let use = document.createElementNS(svgns, "use");
        use.setAttributeNS(xlinkns, "href", "#icon1");
        icon.appendChild(use);
        document.body.appendChild(icon)
    </script>
    
</body>
</html>

 希望对你们有帮助!

原文地址:https://www.cnblogs.com/y-y-y-y/p/12488773.html