操作iframe父页面、子页面的元素和方法 js

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

原文地址:https://www.cnblogs.com/easter729/p/13035282.html