操作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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM