javascript 将svg安全加载为xml并将其注入html

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 将svg安全加载为xml并将其注入html相关的知识,希望对你有一定的参考价值。

    function loadSvg(selector, url) {
      console.log('kshd')
      var target = document.getElementById(selector);

      // If SVG is supported
      if (typeof SVGRect != "undefined") {
        // Request the SVG file
        var ajax = new XMLHttpRequest();
        ajax.open("GET", url + ".svg", true);
        ajax.send();

        // Append the SVG to the target
        ajax.onload = function(e) {
          target.innerHTML = ajax.responseText;
        }
      } else {
        // Fallback to png
        target.innerHTML = "<img src='" + url + ".svg' />";
      }
    }

以上是关于javascript 将svg安全加载为xml并将其注入html的主要内容,如果未能解决你的问题,请参考以下文章

如何从服务器加载SVG并将其插入内联

将外部 SVG 图标加载到原生 JavaScript 代码中

通过 XML HttpRequest 加载时 OnClick() 函数不起作用

对象标记加载 SVG 但其内容文档为空

Autodesk 查看器从 SVG 文件加载保存的标记

将 SVG 动画录制并保存为动画 GIF [关闭]