且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字符串创建SVG DOM元素

更新时间:2021-12-26 19:12:17

您可以使用

You can use DOMParser to parse an XML string.

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

已解析的字符串的根元素将为doc.documentElement

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

要使此功能在跨浏览器中正常运行,您需要设置html命名空间,即您的字符串需要如下所示...

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='<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500" ...