jquery - 无法通过 jQuery 在 SVG 中添加图像 : <image/> tag becomes <img/>

标签 jquery svg

我想通过 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/

相关文章:

javascript - `delay` 不是函数 (jQuery 1.3)

javascript - Snap.svg 无法使用 jQuery 动态(并成功)找到附加的 SVG 元素

html - 如何根据视口(viewport)缩放svg

javascript - <a> 标签内的 <a> 标签?

jquery - Google map 街景的可 float div?

javascript - 丢失 jquery 委托(delegate)事件处理程序

javascript - 点击 fancybox 弹出窗口时需要弹出 Fancy 框

javascript - 为什么我的 SVG 通过 javascript 加载为空白?

java - 我可以创建没有 svg 文件的 JSVGCanvas 吗?

javascript - 如何正确使用SVG checkintersection()函数?