javascript 在嵌入的 svg 文件中不起作用

标签 javascript svg

我刚开始学习 SVG。从 Manipulating SVG Documents Using ECMAScript (Javascript) and the DOM 得到这个示例代码.我稍微改变一下:

<!DOCTYPE html>
<html>
<body>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
  <script type="text/ecmascript">
  <![CDATA[
    function changeRectColor(evt) {
      var red = Math.round(Math.random() * 255);
      evt.target.setAttributeNS(null,"fill","rgb("+ red +","+ red+","+red+")");
    }
  ]]>
  </script>
  <g id="firstGroup">
    <rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" onclick="changeRectColor(evt)"/>
    <text x="40" y="100">Click on rectangle to change it's color.</text>
  </g>
</svg>
</body>
</html>

它工作得很好。我移动到单独的 SVG 文件,然后 js 代码停止工作:

<!DOCTYPE html>
<html>
<body>
<object type="image/svg+xml" data="exampl3a.svg" />   
  <script type="text/ecmascript">
  <![CDATA[
    function changeRectColor(evt) {
      var red = Math.round(Math.random() * 255);
      evt.target.setAttributeNS(null,"fill","rgb("+ red +","+ red+","+red+")");
    }
  ]]>
  </script>
</body>
</html>

SVG 文件:exampl3a.svg

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
  <g id="firstGroup">
    <rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" onclick="changeRectColor(evt)"/>
    <text x="40" y="100">Click on rectangle to change it's color.</text>
  </g>
</svg>

我该怎么办?

谢谢 韦斯

最佳答案

如果您将 svg 放入不同的文件,那么它将在另一个文档中,您需要使用 getSVGDocument 绑定(bind)到该文档。是的,这在 Chrome 中仍然不适用于本地文件(仅适用于网站,或者除非 Chrome 使用相应的命令行开关运行)。

SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
  <g id="firstGroup">
    <rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" />
    <text x="40" y="100">Click on rectangle to change it's color.</text>
  </g>
</svg>

HTML

<!DOCTYPE html>
<html>
<body>
<object id='mySvg' type="image/svg+xml" data="example3a.svg" />
<script type="text/ecmascript">
    function changeRectColor(evt) {
        var red = Math.round(Math.random() * 255);
        evt.target.setAttributeNS(null,"fill","rgb("+ red +","+ red+","+red+")");
    }

    var obj = document.getElementById('mySvg');
    obj.addEventListener('load', function() {
        var svgDoc= obj.getSVGDocument();
        var elem = svgDoc.getElementById("myBlueRect");
        elem.addEventListener('click', changeRectColor);
    });
</script>
</body>
</html>

关于javascript 在嵌入的 svg 文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778305/

相关文章:

php - 带有复选框的表单输入字段,反之亦然 - 缺少第一个值

javascript - CSS Transform over SVG 的 getComputedStyle 结果不正确

css - 更改 SVG 背景图像的颜色(Bootstrap 4 轮播)

javascript - Svg : iframe can not delete ! 为什么?

php - Google map API - 标记、MySQL

javascript - 返回 <tbody> 的每个 <tr> 中第一个 <td> 值的数组

javascript - 安全 jQuery Mobile + Phonegap

javascript - 如何从这个复杂的 JavaScript 对象访问值?

iphone - UIWebview 操作 SVG 'on the fly'

angularjs - AngularJS 应用程序中的 Svg ClipPath 与 HTML 基本元素