HTML页面通过JS跨域调用,子传父

父页面:a.html 

代码:

<html>
  <head>
    <script type="text/javascript">
      function test1() {
        alert(1);
      }
      window.addEventListener('message', function(event){
        alert(event.data);
      })
    </script>
  </head>
  <body>
    <input type="button" value="aBtn" onclick="test1()" id="button3">
    <iframe src="b.html"></iframe>
  </body>
</html>

子页面:b.html

代码:

<html>
  <head>
    <script type="text/javascript">
      function test2() {
        window.top.postMessage('子向父发送消息','*');
      }
    </script>
  </head>
  <body>
    <input type="button" value="bBtn" onclick="test2()">
  </body>
</html>

原文地址:https://www.cnblogs.com/yunfeiyang-88/p/11209588.html