我想通过 jQuery 在 SVG 中添加项目。
我可以很好地插入标签,例如 <rect />
或<text />
但我无法插入带有标签 <image />
的图像.
此标签变为<img />
自动,无法显示 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 - 无法通过 jQuery 在 SVG 中添加图像 : <image/> tag becomes <img/>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10261731/