项目中通过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
 
 Js获取iframe子页面全局变量Js获取iframe子页面全局变量
  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
 
 Js获取iframe子页面全局变量Js获取iframe子页面全局变量
  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" );

////

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

 

相关文章:

  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-04-09
  • 2021-11-18
  • 2021-08-05
  • 2022-12-23
猜你喜欢
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-08-15
  • 2022-03-09
  • 2022-12-23
相关资源
相似解决方案