js的BOM

参考w3school手册全部查手册

window属性

三种弹框

<script>
    // 1、警告弹框
    // alert('不要出门不要出门!!!')

    // 2、确认弹框
    // var res = confirm('你确定要抛弃我吗?')
    // console.log(res)

    // 3、输入弹框
    var res = prompt('密码是多少?')
    console.log(res)
</script>

window对象方法可以做广告

主要了解如下(重要)

  • innerWidth窗口文档(内容)宽度

  • innerHeight窗口文档(内容)高度

    <scipt>
        console.log('innerWidth:'+window.innerWidth+',innerHeight:'+window.innerHeight)
    
    </scipt>
    

    image-20210731165319122

  • outerWidth浏览器窗口宽度(包括任务栏和滚动条)

  • outerHeight浏览器窗口高度

    <scipt>
        console.log('outerWidth:'+window.outerWidth+',outerHeight:'+window.outerHeight)
    
    </scipt>v
    

    image-20210731165357579

  • offsetWidth:元素(比如div)的宽

  • offsetHeight:元素(比如div)的高

  • offsetX: 鼠标位置离盒子的大小 在jQuery中offset().left

  • clientX:鼠标距离 body 的大小 在jQuery中pageX

  • offsetLeft:offsetLeft值的获取跟父级元素没关系,而是跟其上一级的定位元素(除position:static;外的所有定位如fixed,relative,absolute)有关系。即为相对于定位父级的偏移量

  • offsetTopoffsetTop值的获取跟父级元素没关系,而是跟其上一级的定位元素(除position:static;外的所有定位如fixed,relative,absolute)有关系。即为相对于定位父级的偏移量


        <style type="text/css">
            #box{
                width: 200px;
                height: 200px;
                background: orange;
                margin: 0 auto;
            }
        </style>
    </head>
    <body>
        <div id="box"></div>
        <script type="text/javascript">

            var box=document.getElementById('box');
            console.log('offsetHeight:'+box.offsetHeight+'offsetWidth:'+box.offsetWidth)
        </script>
    </body>
</html>

image-20210731165448356

screenLeft:窗口左上角距离屏幕左边的距离

screenTop:窗口左上角距离屏幕上边的距离

image-20210731165551460

scollTop:滚动条距离顶部的距离

window:表示当前对象

opener:表示父窗口

<script type="text/javascript">
    alert('screenLeft:'+window.screenLeft+',screenTop'+window.screenTop)
</script>

window方法

alert()警告弹框

confirm()确认弹框

<script type="text/javascript">
   //alert('不要出门')
    var res=confirm('你确定?')
    console.log(res)//确认为true,取消为false
</script>

prompt输入弹框

<script type="text/javascript">
			var  res=prompt('张三?')
			console.log()
		</script>

image-20210731140904932

open()打开一个子窗口


重要先找到对象,在进行操作

父窗口

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body onunload="closeAll()">
		<button value="lightblue" onclick="setColor(this)">蓝色</button>
		<button value="green" onclick="setColor(this)">绿色</button>
		<button value="pink" onclick="setColor(this)">粉色</button>
		<button onclick="newWin.child()">调用子窗口的方法</button>
		<script type="text/javascript">
		
			// 创建子窗口
			var newWin=window.open('test.html','_blank','width=400,height=400,left=300,top=100')
			function setColor(obj){
				// this代表点击的对象button
				console.log(obj)
				
				// 改变本窗口颜色
				window.document.bgColor=obj.value;
				
				// 改变新窗口的颜色
				newWin.document.bgColor=obj.value;
				
			}
			
			function closeAll(){
				// 当关闭这个窗口时,子窗口也要删除
				newWin.close();
			}
		</script>
	</body>
</html>

子窗口

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h1>子窗口</h1>
		<button value="lightblue" onclick="setColor(this)">蓝色</button>
		<button value="green" onclick="setColor(this)">绿色</button>
		<button value="pink" onclick="setColor(this)">粉色</button>
		<script type="text/javascript">
		
			function setColor(obj){
				// openner返回对创建窗口的引入 即父窗口
				// 设置父窗口的颜色
				opener.document.bgColor=obj.value;
				// 设值当前窗口的颜色
				window.document.bgColor=obj.value;
			}
		
		
			function child(){
				alert('子窗口方法')
			}
		</script>
	</body>
</html>

image-20210731150856828

image-20210731151124222

moveTo()移动到指定位置

moveBy()移动多少

父窗口

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body onunload="closeAll()">
		<script type="text/javascript">
		
			
		var newWin=window.open('test.html','_blank','width=400,height=400,left=0,top=0')
		
		// 移动子窗口和变大子窗口到指定大小
		
		setTimeout(function(){
			newWin.moveTo(400,400);
			newWin.resizeTo(500,500)
		},1500)
		
		</script>
	</body>
</html>

子窗口

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h1>子窗口</h1>
		<button value="lightblue" onclick="setColor(this)">蓝色</button>
		<button value="green" onclick="setColor(this)">绿色</button>
		<button value="pink" onclick="setColor(this)">粉色</button>
		<script type="text/javascript">
		
			function setColor(obj){
				// openner返回对创建窗口的引入 即父窗口
				// 设置父窗口的颜色
				opener.document.bgColor=obj.value;
				// 设值当前窗口的颜色
				window.document.bgColor=obj.value;
			}
		
		
			function child(){
				alert('子窗口方法')
			}
		</script>
	</body>
</html>

image-20210731151925783

resizeTo()变大到指定大小

resizeBy()变大设置大小

父窗口

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body onunload="closeAll()">
		<script type="text/javascript">
		
			
		var newWin=window.open('test.html','_blank','width=400,height=400,left=0,top=0')
		
		// 每次移动子窗口和变大子窗口到指定大小
		
		setInterval(function(){
			newWin.moveBy(40,40);
			newWin.resizeBy(50,50)
		},1500)
		
		</script>
	</body>
</html>

子窗口

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h1>子窗口</h1>
		<button value="lightblue" onclick="setColor(this)">蓝色</button>
		<button value="green" onclick="setColor(this)">绿色</button>
		<button value="pink" onclick="setColor(this)">粉色</button>
		<script type="text/javascript">
		
			function setColor(obj){
				// openner返回对创建窗口的引入 即父窗口
				// 设置父窗口的颜色
				opener.document.bgColor=obj.value;
				// 设值当前窗口的颜色
				window.document.bgColor=obj.value;
			}
		
		
			function child(){
				alert('子窗口方法')
			}
		</script>
	</body>
</html>

image-20210731152138634

scrollTo()滚动到指定位置

scrollBy()滚动多少

<script>
    for(var i=0;i<1000;i++){
        document.write('<h3>疫情严重,大家请保重!!!!</h3>')
    }

    // setTimeout(function(){
    // 	window.scrollTo(0,3000)
    // },1500)

    setInterval(function(){
        // window.scrollBy(0,100)
    },300)
</script>

History对象(非常重要)

forward()进入下一条历史记录

back()回到上一条历史记录,登录后用这个方法

go()跳转到指定的历史记录

注意

go(-1)back()效果一致

go(1)forward()效果一致

注意他们前面必须写history

<script type="text/javascript">
    console.log(window.history)

    setTimeout(function(){
        window.history.back(); 
        window.history.go(-1);
    },3000)
</script>

location对象

hash获取#后面的内容

host获取主机名和端口号

hostname获取主机名

pathname返回当前的url路径

protocol返回协议

href获取或设置地址 有历史记录

search获取?后面的内容

<script>
	console.log(window.location)
			
			// host获取主机名和端口名
			console.log(window.location.host)//000.0.0.1:8848
			
			// port获取主机名和端口号
			console.log(window.location.port)//8848
			
			// hostname获取主机名
			console.log(window.location.hostname)//000.0.0.1
			
			// pathname返回当前的url路径
			console.log(window.location.pathname)///CQ2108/js%E5%9F%BA%E7%A1%80/0731/04open%E6%89%93%E5%BC%80%E4%B8%80%E4%B8%AA%E6%96%B0%E7%AA%97%E5%8F%A3.html
			
			// protocol返回协议
			console.log(window.location.protocol)//http:
			
			// href获取或设置地址  有历史记录
			console.log(window.location.href)//一段地址
			
			// hash获取#后的内容
			console.log(window.location.hash)
			
			// search获取?后的内容
			console.log(window.location.search)
</script>

replace用于当前页面到注册页面,注册页面到登录,登录直接go(-1)返回当前页面,因为它没有历史记录

assign加载新的页面 有历史记录

<script>
    setTimeout(function(){

        //window.location.href='test.html';

        // assign加载新的页面  有历史记录
        //window.location.assign('test.html')

        // replace替换为新页面  没有历史记录
        window.location.replace('test.html')
    },1500)
</script>

n.hash)

		// search获取?后的内容
		console.log(window.location.search)
```

replace永远当前页面到注册页面,注册页面到登录,登录直接go(-1)返回当前页面,因为它没有历史记录

assign加载新的页面 有历史记录

<script>
    setTimeout(function(){

        //window.location.href='test.html';

        // assign加载新的页面  有历史记录
        //window.location.assign('test.html')

        // replace替换为新页面  没有历史记录
        window.location.replace('test.html')
    },1500)
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值