我试图在嵌入式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')