zoukankan      html  css  js  c++  java
  • js父页面和子页面相互取值

    iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同。

    一、同域下父子页面的通信

    父页面parent.html

    <html>
    <head>
        <script type="text/javascript">
            function say(){
                alert("parent.html");
            }
            function callChild(){
                myFrame.window.say();
                myFrame.window.document.getElementById("button").value="调用结束";
            }
        </script>
    </head>
    <body>
        <input id="button" type="button" value="调用child.html中的函数say()" onclick="callChild()"/>
        <iframe name="myFrame" src="child.html"></iframe>
    </body>
    </html>

    子页面child.html

    <html>
    <head>
        <script type="text/javascript">
            function say(){
                alert("child.html");
            }
            function callParent(){
                parent.say();
                parent.window.document.getElementById("button").value="调用结束";
            }
        </script>
    </head>
    <body>
        <input id="button" type="button" value="调用parent.html中的say()函数" onclick="callParent()"/>
    </body>
    </html>

    方法调用

    父页面调用子页面方法:FrameName.window.childMethod();

    子页面调用父页面方法:parent.window.parentMethod();

    DOM元素访问

    获取到页面的window.document对象后,即可访问DOM元素

    注意事项

    要确保在iframe加载完成后再进行操作,如果iframe还未加载完成就开始调用里面的方法或变量,会产生错误。判断iframe是否加载完成有两种方法:

    1. iframe上用onload事件

    2. 用document.readyState=="complete"来判断

  • 相关阅读:
    软件工程之项目管理核心框架
    JPA @Column
    centos 安装 nodejs vue 工具链.
    c语言 打印二进制数
    Python import 导入指定目录的某块
    最近的一点思考,关于高手/大师/学霸
    同步与非同步,阻塞与非阻塞。
    Spring MVC 配置
    Java Web框架的基本组件
    add函数
  • 原文地址:https://www.cnblogs.com/Strive-count/p/6634752.html
Copyright © 2011-2022 走看看