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

通过经典<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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!