iframe父子页面通信各种操作

父页面 

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>父页面</title>
	<style>
		body{
			border: 4px solid red;
			box-sizing: border-box;
		}
		.box{
			display: inline-block;
			width: 300px;
			vertical-align: top;
		}
		iframe{
			border: 4px solid green;
			box-sizing: border-box;
			display: inline-block;
		}
	</style>
	<script src="jquery-3.1.1.min.js"></script>
</head>
<body>
	<div class="box">
		父页面
		<p class="p1"></p>
		<button id="btn1">获取子页面的变量</button><span id="f1"></span><br />
		<button id="btn2">执行子页面的方法</button>
	</div>
	<iframe id="iframe1" src="iframechild.html" width="" height=""></iframe>
	<script>
		var babamoney=500;
		function babafun1(){
			$('.p1').html('爸爸当前金额为'+babamoney+'元')
		}
		$("#btn1").click(function(){
			let num=$('#iframe1')[0].contentWindow.qian
			$('#f1').text(num)
		})
		$("#btn2").click(function(){
			$('#iframe1')[0].contentWindow.shownum()
		})
	</script>
</body>
</html>

子页面

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>子页面</title>
	<script src="jquery-3.1.1.min.js"></script>
</head>
<body>
	子页面<br />
	<p class="childp"></p>
	<button id="btn1">获取父页面的变量</button><span id="s1"></span><br />
	<button id="btn2">执行父页面的方法</button><br />
	<button id="btn3">刷新父页面(子页面重新被引用会跟着刷新)</button><br />
	<button id="btn3_1">刷新子页面</button><br />
	<button id="btn4">关闭当前iframe</button><br />
	<button id="btn5">关闭浏览器窗口</button><br />
	<button id="btn6">刷新最顶端对象(子页面重新被引用会跟着刷新)</button><br />
	<script>
		var qian=20
		function shownum(){
			$('.childp').text('儿子的当前金额为'+qian);
		}
		$('#btn1').click(function(){
			$('#s1').text(parent.babamoney)
		})
		$('#btn2').click(function(){
			window.parent.babafun1();
		})
		$('#btn3').click(function(){
			parent.location.reload();
		})
		$('#btn3_1').click(function(){
			window.location.reload(); 
		})
		$('#btn4').click(function(){
			window.parent.document.getElementById("iframe1").style.display="none";
		})
		$('#btn5').click(function(){
			window.parent.close()
		})
		$('#btn6').click(function(){
			top.location.reload();
		})
	</script>
</body>
</html>

 

var dom = window.parent.document.getElementById("menus");
	   			$(dom).find('.nav-notice').hide();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值