duanji1482 2016-06-12 08:35
浏览 757

通过经典<a>链接更改svg中<image>的xlink:href

I have a svg in my webpage (I use php):

<svg width="500px" height="500px" xml:lang="fr"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<image width="500" height="500" xlink:href="img1.jpg" opacity="0.35" />
</svg>

I would like to be able to change the xlink:href variable when clicking on a link (and without reload the webpage), something like:

<a href=#" onclick="changexlinkhref(img2.jpg)">change with img2</a>

But I don't know what would be the code of the js function changexlinkhref(img){} (and for now I don't use jquery on my project)

Thanks!

  • 写回答

2条回答 默认 最新

  • drde3456 2016-06-12 08:47
    关注

    You'll need to put the img2.jpg argument in single quotes Then something like this should do it provided you only have one image element on the page.

    function changexlinkhref(value) {
      document.querySelector("image").setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', value);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器