javascript document.getElementById函数有问题。问题是,除Internet Explorer之外的每个浏览器都出现document.getElementById为null的错误。getElementById在Internet Explorer中返回null
对于实施例的Firefox:
类型错误:的document.getElementById(...)为空
的的getElementById用功能出现按钮的声明之后,因此它不应该是一个问题,该函数不知道ID元素是什么。
这是脚本与有关代码的摘录:
<html>
<head>
<title>Timeline</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" id="myform" onsubmit="window.location.reload();">
<input type="hidden" id="client_timestamp" name="client_timestamp" />
<button name= "subm_myform" type="submit" >Send My Time</button>
</form>
<script type="text/javascript">
// ------- where the error occurs ----------------
document.getElementById('subm_myform').style.visibility='hidden';
var mySync = setTimeout(function() {document.getElementById('subm_myform').click()} ,60000);
</script>
</body>
</html>
谢谢!
'<按钮名称= “subm_myform”>'/'的getElementById( 'subm_myform')' - 说不定IE合并了'name'和'ID '出于一些向后兼容的原因?我在你发布的代码中没有带'id =“subm_myform”'的元素。 – pawel
你的按钮有一个'subm_myform'的名字,而不是一个ID。如果IE没有通过ID找到一个元素,它会试图通过名称来实现。至少在旧版本的IE中。(我们实际上必须将getElementByID函数填充到每个浏览器中,以便我们的一些代码库一次... _shudder_) –