iframe獲取父頁面元素:
JavaScript: $(window.parent.document.getElementById("元素id"))
jquery: $("#元素id", parent.document)
父頁面獲取iframe頁面元素(iframe加載完之后再獲取里面的元素):
JavaScript:window.frames["iframeId"].document.getElementById("元素id")
document.getElementById("iframeId").contentWindow.document.getElementById("元素id")
jquery: $("#元素id", document.frames("iframeId").document)
$('#iframeId').contents().find("#元素id")[0]
iframe獲取父頁面函數方法:
// 先通過window.parent獲取到父元素,在調用該對象上的方法
JavaScript: window.parent.funcName()
jquery: $(window.parent)[0].funcName()
使用layui調用父頁面方法時報錯:funcName is not a function
解決方案:
在父頁面定義一個全局方法:
window.TestFunc= function(){ alert("test"); };
iframe子頁面直接調用: window.parent.TestFunc();
父頁面獲取iframe頁面函數方法:
// 先獲取到子頁面的window對象,然后在調用
window.onload = function() {
var ifra = document.getElementById("ifra");
ifra.contentWindow.funcName();
}
參考:https://www.cnblogs.com/bonly-ge/p/9771167.html
https://www.jb51.net/article/123314.htm
https://blog.csdn.net/csdn_chengpeng/article/details/102959017