首页 新闻 会员 周边

javascript页面加载事件的问题

0
悬赏园豆:10 [待解决问题]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta  http-equiv="X-UA-Compatible"  content="IE=edge, chrome=1">
    <meta  name="renderer"  content="webkit">
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/style.css">
    <title>登录--磁湖在线</title>
</head>
<body class="login-body" id="mark">
    <div class="login-wrap">
        <div class="login">
            <h1><a href="" src="#" class="login-title">CHOnline</a></h1>
            <form action="ddd">
                <input type="text" id="username" class="username" placeholder="用户名">
                <input type="password" id="password" class="password" placeholder="密码">
                <button type="submit" id="login-sub">登 录</button>
                <input type="checkbox" value="1" id="remeber"> 记住我
            </form> 
            <a href="#" class="link">回到首页</a>
            <a href="register.html" class="link">立即注册</a>
        </div>
        <!-- 登陆框内容结束 -->
    </div>
    <script>
        alert(document.getElementById("username").nodeName);//结果为 INPUT
        var name = document.getElementById('username');
        alert(name.nodeName); // 结果为 undefined
        window.onload = function  () {
            var name1 = document.getElementById("username");
            alert(name1.nodeName); //结果为 INPUT
        } 
        
    </script>
</html>

我想知道 为什么会出现那样的结果

lleo小浩的主页 lleo小浩 | 初学一级 | 园豆:200
提问于:2014-10-19 10:21
< >
分享
所有回答(1)
0

var name = document.getElementById('username');

alert(name.nodeName); // 结果为 undefined

把name改为name1,结果为 INPUT。

name不是保留字,只是window对象的一个属性,对于脚本里面,所有window的属性和方法都可以省略window的。

gltide | 园豆:403 (菜鸟二级) | 2014-10-19 11:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册