在js中,我們時常用到用iframe做系統框架,在子頁面也,父頁面之間的值傳遞是一個問題,下面是js獲取父窗體和子窗體的對象js:
1.在iframe子頁面中獲取父頁面的元素:
a>window.parent.document這個是獲取父頁面document中的對象;
- var reportRadio = window.parent.document.getElementById("reportRadio").value;
b>如果要獲取父頁面js中的方法:window.parent.xxxx();xxxx()為方法;
2.在父頁面中獲取iframe子頁面中的元素:
a>
var child = document.getElementById("mainFrame").contentWindow;//mainFrame這個id是父頁面iframe的id
- <span style="white-space:pre"> </span>child.document;//獲取子頁面中的document對象
js怎么獲取iframe的src鏈接到的頁面內的元素,並綁定事件
- window.onload=function(){
- document.getElementById('iframe1').contentWindow.document.getElementById('btn1').onclick=function(){alert(123);return false;};
- };
IFrame下父頁面調用子頁面的JS方法
- parent.frames["frameName"].window.functionName();
frameName:IFrame的ID;
funtionName:子頁面中JS方法名;
var obj = parent.frames["frameName"].window;
在父頁面訪問子頁面中某個控件如:obj.document.getElementByIdx_x('clientID')
子頁面刷新父頁面:top.window.location.replace(Url);
