ホームページ  >  記事  >  ウェブフロントエンド  >  HTML iframe 要素間の呼び出し_html/css_WEB-ITnose

HTML iframe 要素間の呼び出し_html/css_WEB-ITnose

WBOY
WBOYオリジナル
2016-06-24 11:32:271169ブラウズ

HTML iframe 要素間の呼び出し
1. はじめに
通常、独立したページを導入する必要がある場合、iframe を使用します。ビジネスで必要な場合は、親ページと iframe ページの間で対話する必要があります。対話するときは、js または jquery を使用して、親ページまたは子ページの関連要素を操作する必要があります。
2. 例
1. ページの構造は次のとおりです:

親ページ:

 1 <!DOCTYPE html> 2 <html> 3     <head> 4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5         <title>父页面</title> 6     <script  type="text/javascript"   src="jquery-1.8.3.min.js"></script> 7     </head>  8     <body>  9         <input id="username" name="username" value="zhangsan" />10         <button onclick="getIframeEle();">change iframe1 from js</button>11         <button onclick="getIframeEle2();">change iframe1 from jquery</button>12         </br>13         <iframe id="t_sub" name="myFrame"  src="./sub.html"> </iframe> 14         <iframe id="t_sub2" name="myFrame2"  src="./sub2.html"> </iframe> 15     <script type="text/javascript">16     function getIframeEle(){ //通过js父页面操作子页面中的元素17         var childdoc = document.getElementById("t_sub").contentWindow.document;18         var childele = childdoc.getElementById("tname");19         childele.value = "chang from parent" + new Date();20         console.log(childele.value);  21     }22     function getIframeEle2(){//通过jquery父页面操作子页面中的元素23         //console.log($(document.frames("t_sub").document)); 24         var childele = $(document.getElementById('t_sub').contentWindow.document).find("#tname");25         childele.val("jquery iframe");26         console.log(childele.val());27     }28 29 30     // 计算页面的实际高度,iframe自适应会用到31   function calcPageHeight(doc) {32       var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)33       var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)34       var height  = Math.max(cHeight, sHeight)35       return height36   }37   var ifr = document.getElementById('t_sub2')38   ifr.onload = function() {39       var iDoc = ifr.contentDocument || ifr.document40       var height = calcPageHeight(iDoc)41       ifr.style.height = height + 'px'42   }43     </script>44     </body>45 </html>

サブページ 1

 1 <!DOCTYPE html> 2 <html> 3     <head> 4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5         <title>子页面一</title>  6     <script  type="text/javascript"   src="jquery-1.8.3.min.js"></script> 7     </head>  8     <body>  9         <input id="tname" value="test" />10         <button onclick="getParentEle();">change parent from js</button>11         <button onclick="getBrothertEle2();">change brother iframe from js</button>12         <button onclick="getParentEle2();">change parent iframe from jquery</button>13 <script type="text/javascript">14 <!--15 function getParentEle(){//通过js获取并操作父页面的元素16     var par_input =    parent.document.getElementById("username");17     par_input.value = "change from iframe" + new Date();18     console.log(par_input.value)19 }    20 function getParentEle2(){//通过jquery获取并操作父页面中的元素21     var usernameELe = $('#username', parent.document);22     usernameELe.val("change from child iframe ");23     console.log(usernameELe.val());24 }25 function getBrothertEle2(){//通过js获取并操作兄弟iframe中的元素26     var parentDOM = parent.document.getElementById("t_sub2").contentWindow.document;;27     var borele = parentDOM.getElementById('tname2');28     borele.value = "change from brother iframe" + new Date();29     console.log(borele.value)30 }31 //-->32 </script>33     </body>34 </html>

サブページ 2

 1 <!DOCTYPE html> 2 <html> 3     <head> 4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5         <title>子页面二</title>  6     </head>  7     <body>  8         <input id="tname2" value="test22" />  9         <a href="javascript:void(0);" onclick="test(this);" >test</a>10         <br/><br/><br/><br/><br/><br/><br/> 11     <script type="text/javascript">12     function test(this_){13         window.parent.location.href="http://www.baidu.com";14     }15     </script>        16     </body>17 </html>

ページの効果は次のとおりです。以下に示すように:

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。