简体   繁体   English

将SVG字符串附加到dom

[英]appending SVG string to dom

I'm trying to append a string of svg elements to the dom. 我正在尝试将svg元素字符串添加到dom。 This is my setup. 这是我的设置。

var oFragment = '';
for (var i = 0; i < 10; i++) {
oFragment += '<g><path id="note-'+i+'" d="M 6,3 84,6  c 0,0 -6,76 14,91  L 58,97 19,89  c 0,0 -24,-5 -13,-86 z" style="fill:#ffc835;" /></g> ';
}

Here is what i tried. 这是我尝试过的。 It gives the following error: "parseXML is not defined" 它给出以下错误:“未定义parseXML”

var oSVG = document.getElementById("svg-wall").getSVGDocument();
var oNotes = oSVG.getElementById('notes');
oNotes.appendChild(parseXML(oFragment, document));

So my question is: what am i doing wrong and is this even the best way to append a svg string to the dom? 所以我的问题是:我在做错什么,这甚至是将svg字符串附加到dom的最佳方法吗?

parseXML is part of SVG Tiny 1.2 , but the SVGGlobal interface methods are not supported in any web browsers at the moment AFAIK. parseXMLSVG Tiny 1.2的一部分,但AFAIK目前不支持任何Web浏览器使用SVGGlobal接口方法。 It's entirely possible to implement it using other technologies however. 但是,完全可以使用其他技术来实现它。 If you look at the bottom of this blogpost of mine, SVG at the movies take 2 , you'll find a simple wrapper script that implements parseXML (should work fine in opera, firefox and webkit). 如果看一下我的这篇博客文章的底部, 电影中的SVG为2 ,您会发现一个简单的包装器脚本,该脚本实现了parseXML (在Opera,firefox和webkit中应该可以正常工作)。 For examples of this script being used have a look at the source of the demos in that blogpost. 有关使用此脚本的示例,请查看该博客文章中的演示源。

I don't think there's a free function called parseXML (are you missing some functions?). 我认为没有一个免费的函数叫做parseXML (您是否缺少某些函数?)。

And since you're dealing with DOM anyway, just not directly insert the elements with DOM? 而且由于您还是要处理DOM,是否只是不直接将元素插入DOM?

for (var i = 0; i < 10; ++ i) {
   var path = document.createElement("path");
   path.setAttribute("id", "note-" + i);
   ...
   var g = document.createElement("g");
   g.appendChild(path);
   oNotes.appendChild(g);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM