父子窗口之间的调用

把父窗口数据传到子窗口,并设置到子窗口对应元素上。

open.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parent</title>
<script type="text/javascript">
	var childWindow;
	var i = 0,
		j = 1;
	function getData(){
		childWindow.setData({'a': ++i,'b': ++j});
	}
	
	function openWin(){
		childWindow = window.open('top.html', 'childWindow', 'alwaysRaised=yes,height=400,width=600');
	}
</script>
</head>
<body>
<div>
<button onclick="openWin();">open</button>
</div>
</body>
</html>

top.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>child</title>
<script type="text/javascript">
	function setData(data){
		document.getElementById('a').innerHTML = data.a;
		document.getElementById('b').innerHTML = data.b;
	}
</script>
</head>
<body onload="window.opener.getData();">
<div id="menu">
<button onclick="window.close();">close</button>
</div>
<div>
<label id="a">1</label>
<label id="b">2</label>
</div>
</body>
</html>

如果父窗口调用子窗口的函数报undefined,把DOCTYPE删除掉

猜你喜欢

转载自blog.csdn.net/suwing6406029/article/details/51286448