iframe子页面与父页面间的方法,属性互相调用

以下方式亲测可以使用

1.建议一个动态web工程,本人使用的是eclipse,建立两个jsp页面及一个jquery插件,一个是父页面,一个子页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="static/script/jquery-1.7.2.js"></script>
<title>Insert title here</title>
</head>
<script type="text/javascript">
     var parVal = '我是父页面默认变量值!!';
     function parentToChild(){
    	 //获取子页面窗口对象
    	 var childWin = document.getElementById('frame1').contentWindow;
    	 //调用子页面对象
    	 childWin.childtest();
    	 //获取子页面标签对象,并修改
    	 var childInp1 = childWin.document.getElementById('inp1');
    	 childInp1.value='js修改子页面inp1的值';
     }
     $(function(){
		     $("#btn").click(function(){
		    	 //获取子页面窗口对象
		    	  var childjq = $("#frame1").contents();
		    	 //获取子页面标签对象
		    	  var inp1val = childjq.find("#inp1");
				  //为子标签属性赋值
		    	  inp1val.val('jquery修改子页面inp1的值')
		     });
     });
     
     function childToParent(){
           alert('我是父页面 childToParent 方法,待子页面调用');    	 
     }
</script>
<body>
            <div>父页面</div>
            <div>
                  <input id="pInput1" value="我是父页面-pInput1-的值">
            </div>
            <iframe id="frame1" style="width: 400px;height: 400px;margin-top: 20px" src="child.jsp"></iframe>
            <button onclick="parentToChild()">父页面调用子页面</button>
            <button id="btn" onclick="parentToChildJquery()">父页面调用子页面==jquery</button>
</body>
</html>
<%@ 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">
<script type="text/javascript" src="static/script/jquery-1.7.2.js"></script>
<title>Insert title here</title>
</head>
<script type="text/javascript">
      function childtest(){
         //$("#inp1").val('修改inp1的值');
         alert('子页面供父页面调用,测试方法');    	 
      }
      function childToP(){
    	 // 下面两种操作父页面方法的方式一样
    	 // window.parent.childToParent();
    	  parent.childToParent();
    	  var parInp = window.parent.document.getElementById('pInput1');
    	  parInp.value='通过子页面js赋值';
      }
      
      $(function(){
    	  $("#btn3").click(function(){
    		  //获取父页面标签对象
    		  var parInp = parent.$("#pInput1");
    		  //通过jquery为父页面对象赋值
    		  parInp.val('通过子页面jquery赋值')
    		  //调用父页面方法
    		  parent.childToParent();
    	  }) ;
      });
</script>
<body>
         <div >iframe子页面测试</div>
         <input style="margin-top: 50px" id="inp1" value="通过父页面来修改此值~~~">
         <button id="btn2" onclick="childToP()">js子页面调用父页面</button>
         <button id="btn3">jquery子页面调用父页面</button>
</body>
</html>

è¿éåå¾çæè¿°

### 如何在 iframe 页面 onload 完成后调用父页面方法 为了实现这一功能,可以利用 JavaScript 的 `window.onload` 和跨文档通信机制来完成。以下是具体的做法: #### 方法描述 当页面(即嵌套在 `<iframe>` 中的内容)加载完成后,可以通过设置页面中的 `window.onload` 事件监听器,在该事件触发时通过 `parent` 或 `window.parent` 对象访问并调用父页面方法[^1]。 #### 实现方式 以下是一个具体的例展示如何实现此需求: ##### 父页面 (main.html) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Parent Page</title> <script type="text/javascript"> function parentMethod() { console.log('The child page has finished loading and called the method on the parent.'); } </script> </head> <body> <h1>This is the Parent Page</h1> <iframe id="myIframe" src="childPage.html"></iframe> </body> </html> ``` ##### 页面 (childPage.html) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Child Page</title> <script type="text/javascript"> window.onload = function () { // 当页面完全加载完毕后,调用父页面上的方法 if (window.parent && typeof window.parent.parentMethod === 'function') { window.parent.parentMethod(); } else { console.error('Parent method not found or inaccessible'); } }; </script> </head> <body> <h1>This is the Child Page</h1> </body> </html> ``` 在这个示例中,一旦页面成功加载,它会自动检测是否存在名为 `parentMethod` 的函数,并尝试在其父窗口上调用这个函数[^4]。 #### 注意事项 - **安全性考虑**: 如果父页面位于不同的域上,则可能受到同源策略的影响而无法互相访问对方的对象或属性。 - **错误处理**: 应始终检查目标对象及其成员的存在性和可访问性,以防止运行期异常发生[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值