前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >window和document

window和document

作者头像
鹤川
发布2023-03-21 21:45:12
5590
发布2023-03-21 21:45:12
举报
文章被收录于专栏:鹤川博客

window和document

都是网页中的JavaScript对象。

  • window对象:就是这个浏览器的窗口,可以通过window获取宽度、高度、网页跳转
  • document对象:可以通过函数获取网页中标签,然后通过js操作标签

代码实战

新建 html 文件 20-window.html,编写下方程序,运行看看效果吧

代码语言:javascript
复制
<!DOCTYPE html>
<htmllang="en">
    <head>
        <metacharset="UTF-8">
        <metahttp-equiv="X-UA-Compatible"content="IE=edge">
        <metaname="viewport"content="width=device-width, initial-scale=1.0">
        <title>Window</title>
    </head>
  
    <body>
        <ahref="./19-alert.html">19-alert</a>
        <buttononclick="nav()">跳转</button>

        <inputid="myname"/>
        <buttononclick="getMyName()">获取内容</button>


        <divid="mylight"style="width:50px;height:50px;background-color: black;">灯</div>
        <buttononclick="openLight()">开灯</button>

        <scripttype="text/javascript">
            //alert(window.innerWidth)
            //alert(window.innerHeight)

            function nav(){
                window.location.href ="./19-alert.html"
                // window.location.reload();//页面刷新
            }

            function getMyName(){
                let myname = document.getElementById("myname").value
                alert(myname)
            }

            function openLight(){
                document.getElementById("mylight").style.backgroundColor="red"
            }
        </script>
    </body>
  
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023 年 02 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • window和document
    • 代码实战
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档