实现 iframe 子页面调用父页面中的js方法

时间:2023-03-08 16:25:37
实现 iframe 子页面调用父页面中的js方法
父页面:index.html(使用iframe包含子页面child.html)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. <!--
  5. function toshow(msg){
  6. alert("这里是父页面:"+msg);
  7. }
  8. // -->
  9. </script>
  10. </head>
  11. <body>
  12. <iframe background="blue" src="child.html" mce_src="child.html"></iframe>
  13. </body>
  14. </html>

子页面child.html(调用父页面toshow方法)

    1. <html>
    2. <head>
    3. <script type="text/javascript">
    4. <!--
    5. function doparent(){
    6. window.parent.toshow('ccccchild');
    7. }
    8. // -->
    9. </script>
    10. </head>
    11. <body>
    12. This is child frame!!
    13. <input type="button" value="请点击" onclick='doparent()'></input>
    14. </body>