iframe API用法

代码: parent.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中,直接通过iframe名称.window 即可获得iframe所包含的html页面的window对象。eg: myFrame.window

代码 child.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>

总结: iframe所包含的页面通过 parent.window即可获得父页面的window对象。

猜你喜欢

转载自blog.csdn.net/qq_35176916/article/details/84941145