无法通过jQuery在SVG中添加图像:标签变为

我想通过jQuery在SVG中添加项目。 我可以插入诸如之类的标签,但是我无法插入带有标签的

此标记自动变为 ,无法在SVG中显示图像: http : //jsfiddle.net/G4mJf/

我怎么能阻止这个? 如果不可能,是否有另一种方法可以使用JavaScript / jQuery在SVG中插入图像。

你应该使用createElementNS()

 var img = document.createElementNS('http://www.w3.org/2000/svg','image'); img.setAttributeNS(null,'height','536'); img.setAttributeNS(null,'width','536'); img.setAttributeNS('http://www.w3.org/1999/xlink','href','https://upload.wikimedia.org/wikipedia/commons/2/22/SVG_Simple_Logo.svg'); img.setAttributeNS(null,'x','10'); img.setAttributeNS(null,'y','10'); img.setAttributeNS(null, 'visibility', 'visible'); $('svg').append(img); 

正如这个答案所解释的那样 ,仅jQuery无法正确处理SVG操作。 您可能应该使用Raphael ,或上面链接中描述的hacky方法之一。