外面在下雷 2013-04-25 10:11 采纳率: 100%
浏览 345
已采纳

在SVG中添加了Javascript,但是不能在JSVGCanvas中运行。

我要做的是在svg中更换图片,添加了JS在svg中。
在浏览器上能够很好的运行,但是在蜡染的JSVGCanvas中却没有反应。
有没有办法解决这个问题呢?
先谢谢了!

  • 写回答

1条回答

  • Dead_Knight 2013-04-26 14:00
    关注

    帮你解决了这个问题,因为在浏览器中使用javascript操作svg的元素没有那么规范,而batik操作svg元素需要严格按照w3c的svg规范,所以svg文件中的方法需要改为:
    [code="java"]
    function dragOn(spath){
    var imgView = document.getElementById("imgView");
    imgView.setAttributeNS( "http://www.w3.org/1999/xlink", "href", spath );
    }
    [/code]
    我已经测试过,具体可参考:
    [url]http://www.w3.org/Graphics/SVG/WG/wiki/Href[/url]
    下面这些方式中,就最后两个是Correct正确的。
    [code="java"]
    el.setAttribute( "href", "foo.png" );
    el.setAttribute( "xlink:href", "foo.png" );
    el.setAttributeNS( "xlink:href", "foo.png" );
    el.setAttributeNS( "xlink", "href", "foo.png" );
    el.setAttributeNS( xlink, "href", "foo.png" ); (where there is no JS variable called "xlink")
    el.setAttributeNS( "http://www.w3.org/1999/xlink", "href", "foo.png" ); (Correct)
    var xlinkns = "http://www.w3.org/1999/xlink"; el.setAttributeNS( xlinkns, "href", "foo.png" ); (Correct)

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗