<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function check()
{
name=document.getElementById("1");
password=document.getElementById("2");
    alert(name.value);
    alert(password.value);
}
</script>

<body>1<input type="text" id="1" value="1" /><br />
2<input type="text" id="2" value="2"/>
<input type="button" value="登录" onclick="check()" />
</body>
</html>
这段简单的代码,在ff里面都能正常的运行。    
    alert(name.value);
    alert(password.value);
但是在IE(IE6 和IE8)里只有    alert(password.value);这部分正常显示,而    alert(name.value);提示没有定义。怎么回事?知道原因的朋友回答一下啊,谢谢了

解决方案 »

  1.   

    window对象默认有name属性的,不声明的话 name是指 window.name
    换变量名,或者用var声明一下。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <script type="text/javascript">
    function check()
    {
    var name=document.getElementById("t1");
    var password=document.getElementById("t2");
    alert(name.value);
    alert(password.value); 
    }
    </script>
    <body>1<input type="text" id="t1" value="1" /><br />
    2<input type="text" id="t2" value="2"/>
    <input type="button" value="登录" onclick="check()" />
    </body>
    </html>