解決同一頁面中兩個iframe互相調用jquery,js函數


這一個月又沒更新博客,唉,懶癌又犯了,今天解決了一個問題,關於兩個iframe互相調用jquery函數方法

a.html中有兩個iframe,如下:

 <iframe width="100%" height="100%" name="left" scrolling="auto" frameborder="0" src="b.html" id="left"></iframe>

<iframe width="100%" height="100%" name="main1" scrolling="auto" frameborder="0"  src="c.html" id="main1" ></iframe>

b.html中有一個treeview,稱為左菜單iframe

c.html為b.html中treeview點擊后顯示的數據,就稱為右頁面iframe。

既然兩個頁面之間都有關系,存在在a.html中,那這兩個頁面就能互相調用Js方法。

ps:如果兩個頁面之間沒有一點關系,那就讓他們產生關系方能互相調用,比如說兩個頁面都引用一個js文件,那就能互相調用了。

比如說

c.html中有一個button

<input type="button" id="btn" onclick="btnAlert(i)"/>
<input type="hidden" id="hidVal" name="hidVal" />
function btnAlert(index) {
   $(window.parent.$("#left"))[0].contentWindow.GetIndex(index);//調用b.html中的方法
}

b.html中

 function GetIndex(index) {   
  $(window.parent.$("#main1")).contents().find("#hidVal").val(index);//找到c.html中的hidVal給他賦值 }

最后總結下:

如果是c.html的iframe是在b.html中那么引用下面的方法

1,子iframe內調用父類函數方法:

window.parent.func();

2,子Iframe中獲取父界面的元素:

$("#xx", window.parent.document);

這個xx就是父界面中要獲取的元素的ID。

3,jquery 調用子iframe頁面中js的方法:

 iframefunction()是子頁面的方法

  $(window.parent.document).contents().find("#iframename")[0].contentWindow.iframefunction(); 


免責聲明!

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



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