[HTML]HTML架構IFrame下利用JS在首頁面和子頁面間傳值

來源:互聯網
上載者:User

標籤:

今天寫的程式涉及到JS架構傳值的問題,這些是我找到的一些資料
 
下面首頁面和子頁面互相傳值的DEMO 如果僅僅需要子頁面觸發首頁面的函數 僅需 [ parent.window.你的函數 ] 就可以了
 
DOM方法:
父視窗操作IFRAME:window.frames["iframeSon"].document
IFRAME操作父視窗: window.parent.document

jquery方法:
在父視窗中操作 選中IFRAME中的所有輸入框: $(window.frames["iframeSon"].document).find(”:text”);
在IFRAME中操作 選中父視窗中的所有輸入框:$(window.parent.document).find(”:text”);

iframe架構的HTML:<iframe src=”test.html” id=”iframeSon” width=”700″ height=”300″ frameborder=”0″ scrolling=”auto”></iframe>
細心的朋友一下就能理解,原理其實很簡單,就是用到了$(DOM對象)轉換成jquery對象。


例子:
首頁面:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>首頁面</title>    <script type="text/javascript" src="jquery/jquery-1.4.2.min.js"></script>    <script type="text/javascript">        function showSubValue(){            alert( window.frames[0].document.getElementById("zhe").value);//var v = window.frames[0].document.getElementByIdx_x("subdiv1").innerHTML;//alert(v);//   http://hi.baidu.com/bigideaer/blog/item/780337e6af39933d2df534ff.html           // var o = $(window.frames[0].document).find(":div#subdiv1");           // alert(o.html());        }    </script></head><body><div id="mainDiv">首頁面測試資料</div><input type="button" value="查看子頁面資料" onclick="showSubValue();"/><iframe src="zi.html" width="300" height="300"></iframe></body></html>

 



子頁面:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <script type="text/javascript" src="jquery/jquery-1.4.2.min.js"></script>    <title>子頁面</title>    <script type="text/javascript">        function showMainValue(){//dom方式var v = window.parent.document.getElementById("mainDiv").innerHTML;alert(v);window.parent.document.getElementById("mainDiv").innerHTML = "修改後的首頁面資料";//jquery方式//            var o = $(window.parent.document).find(":div#mainDiv");  //          alert(o.html());        }    </script></head><body><div id="subdiv1" >子頁面測試資料</div><input type="button" id="zhe" value="顯示父頁面資料" onclick="showMainValue();"/></body></html>

 

[HTML]HTML架構IFrame下利用JS在首頁面和子頁面間傳值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.