我在我的 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
最佳答案
function OnLoad()
应为onload = function ()
(区分大小写!)- 您应该使用
setInterval
而不是重复调用setTimeout
- 您应该向其传递一个函数,而不是要评估的字符串
- getElementById 方法没有“.”里面有
- getElementById 方法仅存在于文档对象上
- 除某些版本的 IE 之外,您无法使用
document.id_of_element
获取元素(请改用 getElementById) setAttribute
中间有一个大写的 A- 您可能应该使用其中许多方法的 NS 版本,因为您似乎正在使用混合命名空间文档
- 如果您不打算使用随机数,则不应生成它
关于javascript - 在 HTML 中访问 SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2721934/