iframe父子兄弟之間調用傳值(contentWindow && parent)

學習一個知識點最好的方法就是自己動手去的實現一些簡單的helloworld,雖然可以在網上可以找到很多總結,但自己實現的過程纔是自己的,期間遇到問題並解決問題的過程也會讓自己記憶理解更加深刻。

 更多信息,請參考:http://www.cxyeye.com/

iframe的調用包括以下幾個方面:(調用包含html dom,js全局變量,js方法)

主頁面調用iframe;

iframe頁面調用主頁面;

主頁面的包含的iframe之間相互調用;

 

主要知識點

1:document.getElementById("ii").contentWindow 得到iframe對象後,就可以通過contentWindow得到iframe包含頁面的window對象,然後就可以正常訪問頁面元素了;

2:$("#ii")[0].contentWindow  如果用jquery選擇器獲得iframe,需要加一個【0】;

3:$("#ii")[0].contentWindow.$("#dd").val() 可以在得到iframe的window對象後接着使用jquery選擇器進行頁面操作;

4:$("#ii")[0].contentWindow.hellobaby="dsafdsafsdafsdafsdafsdafsadfsadfsdafsadfdsaffdsaaaaaaaaaaaaa"; 可以通過這種方式向iframe頁面傳遞參數,在iframe頁面window.hellobaby就可以獲取到值,hellobaby是自定義的變量;

5:在iframe頁面通過parent可以獲得主頁面的window,接着就可以正常訪問父親頁面的元素了;

6:parent.$("#ii")[0].contentWindow.ff; 同級iframe頁面之間調用,需要先得到父親的window,然後調用同級的iframe得到window進行操作;

 

源碼

源碼包含內容,主頁面(main.html)中含有兩個iframe子頁面(frame.html,newIframe.html)

  1. 主頁面如何調用子頁面中的方法;
  2. 子頁面如何調用主頁面中的方法;
  3. 兩個子iframe之間如何如何進行交互

main.html

複製代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>顯示圖表</title>
<script src="http://www.cnblogs.com/http://www.cnblogs.com/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var gg="dsafdsafdsafdsafsdaf";
    function ggMM() {
        alert("2222222222222222222222222222222");
    }
    function callIframeMethod() {
        //document.getElementById("ii").contentWindow.test();
        $("#ii")[0].contentWindow.test(); //用jquery調用需要加一個[0]
    }
    function callIframeField() {
        alert($("#ii")[0].contentWindow.ff);
    }
    function callIframeHtml() {
        alert($("#ii")[0].contentWindow.$("#dd").val());
        //alert($("#ii")[0].contentWindow.document.getElementById("dd").value);
        //alert($("#ii")[0].contentWindow.document.getElementById("dd").value);                
    }    
    function giveParameter() {
        $("#ii")[0].contentWindow.hellobaby="dsafdsafsdafsdafsdafsdafsadfsadfsdafsadfdsaffdsaaaaaaaaaaaaa";
    }
</script>
</head>
<body>
    <a href="#" onClick="giveParameter();">參數傳遞</a>
    <a href="#" onClick="callIframeMethod();">調用子iframe方法</a>
    <a href="#" onClick="callIframeField();">調用子iframe變量</a>
    <a href="#" onClick="callIframeHtml();">調用子iframe組件</a></br>    
    <iframe id="ii" src="frame.htm" width="100%" frameborder="0"></iframe>
    <iframe id="new" src="newFrame.htm" width="100%" frameborder="0"></iframe>
</body>
</html>
複製代碼

frame.htm

複製代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>顯示圖表</title>
<script src="http://www.cnblogs.com/http://www.cnblogs.com/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

var ff="adfdasfdsafdsafdsaf";
function test() {
    alert($("#dd").val());
}
function callMainField() {
    alert(parent.gg);
}
function callMainMethod() {
    parent.ggMM();
}
function callMainHtml() {
    alert(parent.$("#ii").attr("id"));
}
function getParameter() {
    alert(window.hellobaby);
}
</script>
</head>
<body>
    <a href="#" onClick="getParameter();">接受參數</a>
    <a href="#" onClick="callMainMethod();">調用子iframe方法</a>
    <a href="#" onClick="callMainField();">調用主窗口變量</a>
    <a href="#" onClick="callMainHtml();">調用子iframe組件</a>    
    <input id="dd" type="text" value="1111111111"/>
</body>
</html>
複製代碼

兄弟iframe頁面 newIframe.htm

複製代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>顯示圖表</title>
<script src="http://www.cnblogs.com/http://www.cnblogs.com/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function callLevelFrame() {
    var ff=parent.$("#ii")[0].contentWindow.ff;
    alert(ff);
}
</script>
</head>
<body>
    <a href="#" onClick="callLevelFrame();">調用兄弟iframe</a>    
    <input id="nn" type="text" value="sdafsdfsa"/>
</body>
</html>
複製代碼

 

結語:

我對jquery html css的理解還需要很多提高,自己總結的東西對大牛來說也很幼稚,但大牛之路都是如此總結向上提高的,我不怕這種幼稚。

更多信息,請參考:http://www.cxyeye.com/

發佈了5 篇原創文章 · 獲贊 10 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章