日期:2014-05-16  浏览次数:20380 次

JavaScript窗口之间值传递—框架之间传值

frame框架之间传值

parent.html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<frameset cols="250, *" frameborder="1">
	<frame src="left.html" name="left" />
    <frame src="right.html" name="right" />
</frameset>
</html>?

?

left.html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
	用户名:<input type="text" name="username"/>
</body>
</html>

?

?

right.html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
	function setValueToLeft() {
		// window对象表示装载该网页的那个帧窗口,这个window对象的parent属性对应整个浏览器窗口对象,
		// 整个浏览器窗口对象的frames数组属性的第0个元素就是左边帧窗口
		var username = window.parent.frames[0].document.getElementsByName("username")[0];
		username.value = "keveon";
		alert(username.value);
	}
</script>
</head>

<body>	
	<input type="button" value="设值" onclick="setValueToLeft()" />
</body>
</html>

?

iframe框架之间传值

parent.html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
	function setValueToChild() {
		// frames表示当前窗口所拥有的子窗口的属性,是一个数组
		// 获取第一个子窗体即<iframe src="child.html"></iframe>
		var age = window.frames[0].document.getElementsByName("age")[0];
		age.value = "20";
	}
</script>
</head>

<body>
	<h1>这是父窗体</h1>
    <iframe src="child.html"></iframe>
    <hr />
    用户名:<input name="username" /><br />
    <input type="button" value="给子窗体赋值" onclick="setValueToChild()" />
</body>
</html>

?

child.html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
	function setValueToParent() {
		// 通过window.parent获取父窗体对象
		var username = window.parent.document.getElementsByName("username")[0];
		username.value = "keveon";	
	}
</script>
</head>

<body>
	<h3>这是子窗体</h3>
	年龄:<input name="age" />
    <input type="button" value="给父窗体赋值" onclick="setValueToParent()" />
</body>
</html>

?

?