我想通过jQuery在SVG中添加项目。我可以很好地插入诸如<rect />或<text />之类的标记,但不能使用标记<image />插入图像。
这个标记自动变成<img />,它不能在SVG:http://jsfiddle.net/G4mJf/中显示图像
我如何防止这种情况发生?如果不可能,有没有其他方法可以使用JavaScript/jQuery在SVG中插入图像。
发布于 2012-04-22 03:01:41
您应该使用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);发布于 2012-04-22 02:43:00
as this answer explains,单独的jQuery不能正确地处理SVG操作。您可能应该使用Raphael,或者上面的链接中描述的一种老套的方法。
https://stackoverflow.com/questions/10261731
复制相似问题