iframe父页子页面调用的方法


1.   当我们在父页面中需要调用iframe标签中嵌入的子页面中的js方法时,可以使用:

     document.getElementById(iframe的id).contentWindow.childtest();

     iframe的id:指的是需要调用的子页面的iframe的id;

     childtest():是子页面中的js方法,夫页面就是要调用子页面的这个方法。

2.当在子页面中需要调用父页面中的方法时,使用如下js:

window.parent.parenttest();

具体代码:

父文件

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Parent Page</title>
    <style>
        h1, input {
            margin-left: 50px
        }
        input {
            border: none;
            background: #317ef3;
            color:#fff;
            border-radius:3px;
            padding:5px 10px;
            margin-bottom:10px;
        }
        #childframe{
            border:1px solid green;
            margin: 0 50px;  }
        #childframe1 {  border: 1px solid #f00; }
    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script language="javascript" type="text/javascript">
      //// 方法1:
      //     //父元素的方法
      //     function parenttest() {
      //         alert("这是父页面的方法!");
      //     }
      //     //调取iframe 子元素页面childtest()的方法

      //     function btnClick1() {
      //          document.getElementById("childframe").contentWindow.childtest();
      //       //   $("#childframe")[0].contentWindow.childtest();
      //     }

       //方法2

       $(function () {
          
           //调取iframe 子元素页面childtest()的方法

           function btnClick1() {
               $("#childframe")[0].contentWindow.childtest();
           }

           $("#btn").click(function () {
               btnClick1()
           })

       })

       //父元素的方法
       function parenttest() {
           alert("这是父页面的方法!");
       }

            
       
    </script>
</head>
<body>
    <div style="margin:auto;">
        <h1 style="font-size:18px;color:green">iframe父子级别互调方法实现</h1>
        <!--<input type="button" id="btn"  value="调用子页面的方法"  onclick="btnClick1()" />-->
        <input type="button" id="btn" value="调用子页面的方法"  />
    </div>
    <div style="margin:auto;">
        <iframe style="width:300px; height:300px;" id="childframe" src="ChildPage.html"></iframe>
        <iframe style="width:300px; height:300px;" id="childframe1" src="ChildPage1.html"></iframe>
    </div>
</body>
</html>

  子文件

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Child Page</title>
    <style>
        h1{ font-size:18px; color:gold;}
        input {
            border: 1px solid #317ef3;
            background: none;
            color: #317ef3;
            border-radius: 3px;
            padding: 5px 10px;
            margin-bottom: 10px;
        }
    </style>
    <script language="javascript" type="text/javascript">
      //function childtest() {
      //    alert("这是子页面的方法!");
      //  }
        var childtest = function () {
            alert("这是子页面的方法!");
        }

      function btnClick() {
          window.parent.parenttest();
      }
    </script>
</head>
<body>
    <div style="margin:auto;">
        <h1>This is the Child Page.</h1>
        <input type="button" value="调用父页面的方法" onclick="btnClick()" />
    </div>
</body>
</html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM