欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

frame的主窗口和子窗口之间通信

程序员文章站 2024-01-24 12:05:16
...
在frame中的子页面调用其他子页面的变量和方法:
1. parent.document.frames("子页面name").子页面方法();
2. parent.子页面name.子页面方法();
3. parent.frames["子页面name"].子页面方法();
例如:
1. parent.document.frames("top").tops();
2. parent.top.tops();
3. parent.frames["top"].tops();

在frame中主页面调用其他子页面的变量和方法:
1.子页面name.子页面方法()
2.子页面name.子页面变量

示例如下:
1、index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <frameset rows="80,*" cols="*" frameborder="no" border="0" framespacing="0">
  <frame src="top.jsp" name="top" scrolling="No" noresize="noresize" id="top" />
  <frameset cols="80,*" frameborder="no" border="0" framespacing="0">
    <frame src="a.jsp" name="a" scrolling="No" noresize="noresize" id="a" />
    <frame src="b.jsp" name="b" id="b" />
  </frameset>
</frameset>
<noframes><body>
</body>
</noframes>
</html>



2、top.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>top.jsp</title>
<script type="text/javascript">
	function a(){
		var aFrame = parent.document.frames("a");
		//var aId = aFrame.document.getElementById("aId").innerText;
		//alert(aId);
		aFrame.tree("111");
	}
</script>
</head>
<body bgcolor="blue">
	<div id="top" onclick="a()">top页面</div> 
</body>
</html>


3、a.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript">
	function tree(d){
		alert(d);
	}
</script>
</head>
<body bgcolor="red">
	<div id="aId">a页面</div>
</body>
</html>


4、b.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body bgcolor="green">
	<div id="b">b页面</div>
</body>
</html>
  • frame的主窗口和子窗口之间通信
            
    
    博客分类: Java Web技术分析  
  • 大小: 35.9 KB