如何替换嵌入式 svg 中的图像?



我正在尝试动态替换嵌入式 svg 中的图像,我可以获取当前的 href 属性,但将其设置为新属性不会改变任何内容。这甚至有效吗,因为它不是通常的 html img 标签?

这里有一个(简化为基本要素(代码摘录:

<script>
alert(document.getElementById("image1").getAttribute('href'));
// this works but document.getElementById("image1").setAttribute('href')='newimage.jpg'; doesn´t do anything
</script>
<svg>
<image id="image1" href="test.jpg"></image>
</svg>

试试这个

document.getElementById("image1").setAttribute('href', 'newimage.jpg')

最新更新