子頁面獲取父頁面的id=care的子頁面
parent.care.location.reload();
父頁面獲取id=imp的子頁面
imp.location.reload();
jquery在iframe子頁面獲取父頁面元素和方法代碼如下:
parent.$("selector");
parent.method();jquery在父頁面獲取iframe子頁面的元素和方法
代碼如下:
iframe.$("select");
iframe.method();
3.js在iframe子頁面獲取父頁面元素代碼如下:
window.parent.document.getElementById("元素id");
4.js在父頁面獲取iframe子頁面元素代碼如下:
window.frames["iframe_ID"].document.getElementById("元素id");
方法調用
父頁面調用子頁面方法:FrameName.window.childMethod();
子頁面調用父頁面方法:parent.window.parentMethod();
DOM元素訪問
獲取到頁面的window.document對象后,即可訪問DOM元素
//iframe高度
function setIframeHeight(iframe) {
if (iframe) {
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
if (iframeWin.document.body) {
iframe.height = iframeWin.document.getElementById('frameContent').scrollHeight +20 || iframeWin.document.body.scrollHeight;
}
}
};
//iframe渲染完后再再計算高度
var iframe= document.getElementById('menuFrame');
iframe.onload = function () {
setIframeHeight(iframe);
};