02JsDom--获取页面元素

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/moxiehutou/article/details/49826649

1.整个页面或者说窗口就是一个window对象—————window是顶级对象
2.页面中定义的变量和方法都是window的
3.使用window对象的属性、方法的时候可以省略window。
比如:
window.alert(‘hello’);
window.f1();
window.document…
4.能不写window就不要写,这样可以减少js文件的字节数。

代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">
        var n=10;
        function f1(){
            return alert('aaa');
        }
        //f1();//内部调用方法
        //alert(n);//内部调用字段
    </script>
</head>
<body>
<form id="fm">
    <input type="button" id="btn" value="确定" />
</form>

<script type="text/javascript">

    //alert(window.document.getElementById('fm'));
    //alert(document.getElementById('btn').value);

    //外部用Window调用
    //window.f1();
    //alert(window.n);
    //alert(window.fm);
    alert(window.fm.btn.value);//window可省略
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/moxiehutou/article/details/49826649