猿问

如何替换嵌入式 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>


慕斯709654
浏览 93回答 1
1回答

MYYA

尝试这个document.getElementById("image1").setAttribute('href',&nbsp;'newimage.jpg')
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答