<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>test</title>
<script type="text/javascript">
function t(){
alert(document.getElementById("test").value);
}
</script>
</head>
<body>
<form action="" method="get">
<input name="test" type="text" value="hello" />
<input type="button" onclick="javascript:t();" value="测试" />
</form>
</body>
</html>
对于如上代码,在不遵守W3C的情况下,在IE下可以正常执行,但是在FireFox下会报错。
因为javascript函数中用的是getElementById,但html中并没有id为"test"的元素。
这时IE会自动寻找name为"test"的元素,但firefox并不会这样“聪明”,它会报错。
但时如果遵守W3C协议的话(在html标签前加上" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ")则上述代码在两个浏览器下都无法执行。
如果给name为test的标签加上id="test" 则无论是否遵守w3c,上述代码在两个浏览器下都可以正常执行。
希望我的上述发现能对大家有帮助。
此文章由 http://www.ositren.com 收集整理 ,地址为:
http://www.ositren.com/htmls/67608.html