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>

posted @ 2019-07-18 19:47  云飞扬-88  阅读(257)  评论(0编辑  收藏  举报