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

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

a.html中有兩個iframe,以下:jquery

 <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。spa

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

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

好比說blog

c.html中有一個buttoninput

<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中iframe

 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(); 

相關文章
相關標籤/搜索