通过JS在svg中添加图像命名空间仍然没有;不要给我看那张照片

adding image namespace in svg through JS still doesn't show me the picture

本文关键字:那张照片 我看 svg JS 添加 命名空间 图像 通过      更新时间:2023-09-26

在通过图片的缩放版本获得参数后,我正在尝试通过Javascript在SVG中添加带有原始大小参数的图片。

Firebug向我展示了元素和所有必要的参数,但带着最美好的祝愿,我没有通过。

this.svg = document.getElementsByTagNameNS('http://www.w3.org/2000/svg','svg');     
var bild = document.createElementNS('http://www.w3.org/2000/svg','image');
var BildURL = this.image[0][0].getAttribute('xlink:href');
var imgX = new Image();
imgX.src = BildURL;
bild.setAttribute("x","60");
bild.setAttribute("y","40");
bild.setAttribute("width",imgX.width);
bild.setAttribute("height",imgX.height);    
bild.setAttribute("id","image12976");
bild.setAttribute("xlink:href",BildURL);
this.svg[0].appendChild(bild);

如果我看一下Firebug,元素是完全存在的。

<image id="image12976" x="60" y="40" width="300" height="430" xlink:href="img/Akira1.jpg">

我把鼠标移到权杖上,我看到了"投资模式"下的矩形图片,但没有看到内容。

这里有人能告诉我我做错了什么吗?!

我非常感谢你。

Tamer

您永远不应该对带有前缀的属性使用getAttribute/setAttribute,有关原因的更多详细信息,请参阅DOM 3 Core。

如果你使用,它会很好用

getAttributeNS('http://www.w3.org/1999/xlink', 'href')

CCD_ 2。

尝试使用null命名空间创建图像:

var bild = document.createElementNS(null, 'image');