window和document
都是网页中的JavaScript对象。
-
window对象:就是这个浏览器的窗口,可以通过window获取宽度、高度、网页跳转
-
document对象:可以通过函数获取网页中标签,然后通过js操作标签
代码实战
代码的详细解读,可以参考视频教程。
新建 html 文件 20-window.html
,编写下方程序,运行看看效果吧
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Window</title>
</head>
<body>
<a href="./19-alert.html">19-alert</a>
<button onclick="nav()">跳转</button>
<input id="myname" />
<button onclick="getMyName()">获取内容</button>
<div id="mylight" style="width:50px;height:50px;background-color: black;">灯</div>
<button onclick="openLight()">开灯</button>
<script type="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>