Js获取iframe子页面全局变量


项目中通过iframe内嵌了一个子页面,子页面定义了一些全局变量,父页面需要获取子页面的全局变量,做了一些测试(我的环境IE10和Firefox32.0.3),得出如下结论:

  IE下: window.frames['iPage'].变量名
  火狐下:window.frames['iPage'].contentWindow.变量名
  IE&火狐下:document.getElementById('iPage').contentWindow.变量名

 具体原因没有深究,今天在调试一个iframe例子的时候发现了问题所在(iframe没有定义name属性),下面做了一个实例

 父页面 parent.html

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <script>  
  3.   function test(){  
  4.     var obj1=document.getElementById("myframe");//获得对应iframe的HTMLIFrameElement对象,可以获取iframe相关属性  
  5.     alert(obj1.src);  
  6.       
  7.     /*情况一:iframe定义了name="myframe"的情况 IE和火狐兼容*/  
  8.     var obj2=window.frames["myframe"];//获得对应iframe的window对象  
  9.     alert(obj2.company);  
  10.     alert(obj2.document.myform.username.value);  
  11.       
  12.     alert(obj1.contentWindow==obj2);//true,可见document.getElementById("myframe").contentWindow与document.frames["myframe"]都是指向iframe对应的window对象  
  13.   alert(obj1.contentWindow.document.myform.username.value);  
  14.     
  15.     
  16.   /*情况二:iframe没有定义name="myframe"的情况,情况如同文章最开始所描述  
  17.     var obj2=window.frames["myframe"];//获得对应iframe的window对象  
  18.     alert(obj2.company);//IE下正确,火狐下错误  
  19.     alert(obj2.contentWindow.company);//IE下错误,火狐下正确  
  20.     alert(obj2.contentWindow.document.myform.username.value);//IE下错误,火狐下正确  
  21.       
  22.     alert(obj1==obj2);//true,可见document.getElementById("myframe")与document.frames["myframe"]都是指向iframe对应的HTMLIFrameElement对象  
  23.   */  
  24.   }  
  25. </script>  
  26. <body onload="test()">  
  27.   <iframe id="myframe" name="myframe" src="child.html" frameborder="3" style="width:300;height:200;border-width:1;border-color:red;border-style:solid"></iframe>  
  28. </body>  
  29. </html>  

子页面 child.html

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. <html>  
  2.  <script>  
  3.    var company="cppei";  
  4.  </script>  
  5. <body>  
  6.   <form name="myform">  
  7.     用户名:<input type="text" name="username" value="test" />  
  8.   </form>  
  9. </body>  
  10. </html>  

最后得出的结论是:要在父页面访问iframe内子页面的全局变量,

1、在iframe 定义了name属性的情况下
  IE&火狐下: window.frames['iPage'].变量名
  IE&火狐下:document.getElementById('iPage').contentWindow.变量名

  因为此时 window.frames['iPage']==document.getElementById('iPage').contentWindow

2、在iframe 没有定义name属性的情况下

  IE下: window.frames['iPage'].变量名
  火狐下:window.frames['iPage'].contentWindow.变量名
  IE&火狐下:document.getElementById('iPage').contentWindow.变量名

  因为此时火狐下window.frames['iPage']==document.getElementById('iPage'),所以要加上contentWindow来访问变量

所以定义iframe时还是最好加上name属性

 ////

query取得iframe中元素的几种方法

 

在iframe子页面获取父页面元素
代码如下:

$('#objId', parent.document);
// 搞定...


在父页面 获取iframe子页面的元素
代码如下:

$("#objid",document.frames('iframename').document)

 

 

 

$(document.getElementById('iframeId').contentWindow.document.body).html()
 
 显示iframe中body元素的内容。

 
$("#testId", document.frames("iframename").document).html();

 根据iframename取得其中ID为"testId"元素


$(window.frames["iframeName"].document).find("#testId").html()

 


用JS或jQuery访问页面内的iframe,兼容IE/FF 
注意:框架内的页面是不能跨域的!

假设有两个页面,在相同域下.

index.html 文件内含有一个iframe:

XML/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=gb2312" />  
<title>页面首页</title>  
</head>  
  
<body>  
<iframe src="iframe.html" id="koyoz" height="0" width="0"></iframe>  
</body>  
</html>   
iframe.html 内容:

XML/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=gb2312" />  
<title>iframe.html</title>  
</head>  
  
<body>  
<div id="test">www.koyoz.com</div>  
</body>  
</html>  

1. 在index.html执行JS直接访问:

JavaScript代码
document.getElementById('koyoz').contentWindow.document.getElementById

('test').style.color='red'   
通过在index.html访问ID名为'koyoz'的iframe页面,并取得此iframe页面内的ID为'test'的

对象,并将其颜色设置为红色.

此代码已经测试通过,能支持IE/firefox .

2. 在index.html里面借助jQuery访问:

JavaScript代码
$("#koyoz").contents().find("#test").css('color','red');   
此代码的效果和JS直接访问是一样的,由于借助于jQuery框架,代码就更短了.
 

收集网上的一些示例:
用jQuery在IFRAME里取得父窗口的某个元素的值
只好用DOM方法与jquery方法结合的方式实现了

1. 在父窗口中操作 选中IFRAME中的所有单选钮
$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");

2. 在IFRAME中操作 选中父窗口中的所有单选钮
$(window.parent.document).find("input:radio").attr("checked","true");

父窗口想获得IFrame中的Iframe,就再加一个frames子级就行了,如:
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");

////

原文链接:http://blog.csdn.net/cuihaiyang/article/details/40587075

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM