iframe相互調用詳解

嵌入iframe機制,不可避免的要用到各個iframe頁面之間方法和屬性的相互調用。javascript

這裏設定有3個頁面,一個父頁面main.html,它嵌入了兩個iframe,分別是:childPage1.html和childPage2.htmlhtml

main.html有一個函數叫parentFunc()。main.html代碼以下:java

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <script type="text/javascript"
    function parentFunc(){
        //代碼段
    }
    </script>
    <body>
        <IFRAME scrolling="no" frameBorder=0 id=frmchild1 name=frmchild1 height="400"
        src="childPage1.html" width="100%" allowTransparency="true"></IFRAME>

        <IFRAME scrolling="no" frameBorder=0 id=frmchild2 name=frmchild2 height="400"
        src="childPage2.html" width="100%" allowTransparency="true"></IFRAME>
    </body>
</html>

子頁childPage1.html中有函數childFunc1()。代碼:函數

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <script type="text/javascript"
    function childFunc1(){
        //代碼段
    }
    </script>
    <body>
    </body>
</html>

子頁childPage2.html中有函數childFunc2()。代碼:spa

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <script type="text/javascript"
    function childFunc2(){
        //代碼段
    }
    </script>
    <body>
    </body>
</html>
子頁調父頁方法

若是子頁childPage1.html要調用父頁main.html中的parentFunc()方法,那麼應該在childPage1.html中寫以下代碼:code

parent.parentFunc()

或者用xml

top.parentFunc()

parent找上一級,top找最上一級。由於childPage1.html屬於二級,因此parent和top做用同樣。 若是 childPage1.html又嵌了一個 grandchildrenPage.html的iframe,想要調用main.html中的parentFunc()方法, 則應該htm

parent.parent.parentFunc()

或者用對象

top.parentFunc()
兩個子頁方法互調

若是childPage1.html要調用childPage2.html中的方法childFunc2(),則應該在childPage1.html中寫以下代碼:ip

parent.frmchild2.parentFunc()

或者用

top.frmchild2.parentFunc()

frmchild2是main.html中引入第二個iframe時的id。同理,childPage2.html要調用childPage1.html中的方法childFunc1(),則應該在childPage2.html中寫以下代碼:

parent.frmchild1.parentFunc()

或者用

top.frmchild1.parentFunc()
子頁取得父頁的節點對象及屬性

在main.html中有一個按鈕:

<input type="button" onclick="testFunc()" value="aaa" id="parentBtn"/>

子頁childPage1.html要取得該按鈕的value,則應在childPage1.html中寫以下代碼:

parent.document.getElementById("parentBtn").value;

或者用

top.document.getElementById("parentBtn").value;
兩個子頁互取節點

例如子頁childPage1.html中有一個按鈕,以下:

<input type="button" value="11" id="btn"/>

若是childPage2.html要調用childPage1.html中按鈕的value,則應該在childPage2.html中寫以下代碼:

parent.frmchild1.document.getElementById("btn").value;

或者用

top.frmchild1.document.getElementById("btn").value;
父頁調用子頁方法

若是main.html要調用子頁childPage1.html中的方法childFunc1(),應在main.html中寫以下代碼:

document.getElementById("frmchild1").contentWindow.childFunc1()

frmchild1是main.html中引入第一個iframe時的id。 注意:可能網上流傳着其餘的作法,但大多不兼容。有的只能在IE生效,有的只能在FireFox下生效。因此要以此爲準。

父頁取得子頁節點對象及其屬性

例如子頁childPage1.html中有一個按鈕,以下:

<input type="button" value="11" id="btn"/>

main.html要取得或更改子頁childPage1.html的按鈕的value值,那麼應該用以下代碼:

document.getElementById("frmrightChild").contentWindow.document.getElementById('btn').value;

再例如要取得子頁childPage1.html的內容高度,能夠從body節點的scrollHeight得到。

document.getElementById("frmrightChild").contentWindow.document.body.scrollHeight;

但要注意須要在iframe頁面onload後才能取到其節點。因此完整的作法是main.html在引入childPage1.html加上onload函數,以下:

<IFRAME scrolling="no" frameBorder=0 id=frmchild1 name=frmchild1 height="400"
src="childPage1.html" onload="getChildHeight()" width="100%" allowTransparency="true"></IFRAME>

getChildHeight()函數以下:

function getChildHeight(){
    var frm=document.getElementById("frmchild1");
    var childHeight;
    childHeight=frm.contentWindow.document.body.scrollHeight+"px";
}
相關文章
相關標籤/搜索