項目中通過iframe內嵌了一個子頁面,子頁面定義了一些全局變量,父頁面需要獲取子頁面的全局變量,做了一些測試(我的環境IE10和Firefox32.0.3),得出如下結論:
IE下: window.frames['iPage'].變量名
火狐下:window.frames['iPage'].contentWindow.變量名
IE&火狐下:document.getElementById('iPage').contentWindow.變量名
具體原因沒有深究,今天在調試一個iframe例子的時候發現了問題所在(iframe沒有定義name屬性),下面做了一個實例
父頁面 parent.html
- <html>
- <script>
- function test(){
- var obj1=document.getElementById("myframe");//獲得對應iframe的HTMLIFrameElement對象,可以獲取iframe相關屬性
- alert(obj1.src);
- /*情況一:iframe定義了name="myframe"的情況 IE和火狐兼容*/
- var obj2=window.frames["myframe"];//獲得對應iframe的window對象
- alert(obj2.company);
- alert(obj2.document.myform.username.value);
- alert(obj1.contentWindow==obj2);//true,可見document.getElementById("myframe").contentWindow與document.frames["myframe"]都是指向iframe對應的window對象
- alert(obj1.contentWindow.document.myform.username.value);
- /*情況二:iframe沒有定義name="myframe"的情況,情況如同文章最開始所描述
- var obj2=window.frames["myframe"];//獲得對應iframe的window對象
- alert(obj2.company);//IE下正確,火狐下錯誤
- alert(obj2.contentWindow.company);//IE下錯誤,火狐下正確
- alert(obj2.contentWindow.document.myform.username.value);//IE下錯誤,火狐下正確
- alert(obj1==obj2);//true,可見document.getElementById("myframe")與document.frames["myframe"]都是指向iframe對應的HTMLIFrameElement對象
- */
- }
- </script>
- <body onload="test()">
- <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>
- </body>
- </html>
子頁面 child.html
- <html>
- <script>
- var company="cppei";
- </script>
- <body>
- <form name="myform">
- 用戶名:<input type="text" name="username" value="test" />
- </form>
- </body>
- </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
