IFrame子窗口JS方法互相调用和传参

来源:互联网 发布:高考失利知乎 编辑:程序博客网 时间:2024/06/16 10:36

child1.html和child2.html都是父页面parent.html中的一个子iframe,请看child1.html如何调用child2.html中的方法。


parent.html

[xhtml] view plaincopy
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.         <meta http-equiv="Content-Style-Type" content="text/css">  
  6.         <meta http-equiv="Content-Script-Type" content="text/javascript">  
  7.         <title>jQuery Cookie Plugin</title>  
  8.     </head>  
  9.     <body>  
  10.         <iframe id="myFrame1" src="child1.html" mce_src="child1.html"></iframe>   
  11.     <iframe id="myFrame2" src="child2.html" mce_src="child2.html"></iframe>  
  12.     </body>  
  13. </html>  


child1.html

[xhtml] view plaincopy
  1. <html>  
  2.     <head>  
  3.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  4.         <meta http-equiv="Content-Style-Type" content="text/css">  
  5.         <meta http-equiv="Content-Script-Type" content="text/javascript">  
  6.         <title>jQuery Cookie Plugin</title>  
  7.     <mce:script type="text/javascript" src="jquery-1.4.2.js" mce_src="jquery-1.4.2.js"></mce:script>  
  8.     <mce:script type="text/javascript"><!--  
  9.     function test1() {  
  10.         window.parent.document.getElementById("myFrame2").contentWindow.test2();  
  11.     }  
  12.       
  13. // --></mce:script>  
  14.     </head>  
  15.     <body>  
  16.         <input type="button" onclick="test1();" value="test" />  
  17.     </body>  
  18. </html>  


child2.html

[xhtml] view plaincopy
  1. <html>  
  2.     <head>  
  3.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  4.         <meta http-equiv="Content-Style-Type" content="text/css">  
  5.         <meta http-equiv="Content-Script-Type" content="text/javascript">  
  6.         <title>jQuery Cookie Plugin</title>  
  7.     <mce:script type="text/javascript" src="jquery-1.4.2.js" mce_src="jquery-1.4.2.js"></mce:script>  
  8.     <mce:script type="text/javascript"><!--  
  9.     function test2() {  
  10.         alert("I am your brother");  
  11.     }  
  12.       
  13. // --></mce:script>  
  14.     </head>  
  15.     <body>  
  16.         2  
  17.     </body>  
  18. </html>  

0 0