【问题标题】:Access SVG inside HTML在 HTML 中访问 SVG
【发布时间】:2010-04-27 14:17:18
【问题描述】:

我在我的 xhtml 文件中使用 SVG。我无法访问 id="Wert" 的文本。 怎么了,我想把颜色从蓝色改成红色。

无标题页面

<script type="text/javascript">
    function OnLoad() {
        setTimeout("timer()", 1000);
    }

    function timer() {
        var randomnumber = Math.floor(Math.random() * 101); // Zahlen von 0..100

        var svgdocument = document.svgid.getSVGDocument( 'svgid');
        svgtext = svgdocument.getElement.ById('Wert');
        svgtext.setattribute('style','fill:red');


        setTimeout("timer()", 1000);
    }
</script>

40

【问题讨论】:

    标签: javascript html svg


    【解决方案1】:
    • function OnLoad() 应该是 onload = function ()(区分大小写!)
    • 您应该使用setInterval 而不是反复调用setTimeout
    • 您应该将其传递给函数,而不是要评估的字符串
    • getElementById 方法没有“.”在里面
    • getElementById 方法只存在于文档对象上
    • 您无法使用document.id_of_element 获取元素,除非在某些版本的 IE 中(使用 getElementById 代替)
    • setAttribute 中间有一个大写字母 A
    • 您可能应该使用其中许多方法的 NS 版本,因为您似乎正在使用混合命名空间文档
    • 如果您不打算使用随机数,则不应生成它

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 2014-12-10
      • 2014-09-28
      • 2016-07-19
      • 1970-01-01
      • 2022-01-19
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多