Creating an SVG DOM element from a String

前端 未结 4 1887
天命终不由人
天命终不由人 2021-02-05 05:40

How would I go about creating an SVG DOM element from a String?

Example:

var svgStr = \'

        
4条回答
  •  梦谈多话
    2021-02-05 06:26

    You can use DOMParser to parse an XML string.

    var parser = new DOMParser();
    var doc = parser.parseFromString(stringContainingXMLSource, "image/svg+xml");
    

    The root element for the parsed string will be doc.documentElement

    For this to work properly cross-browser you'll need to set the html namespace i.e. your string will need to look like this...

    var svg2='

提交回复
热议问题