嵌套iframe頁面中的JS調用

當你打開本頁時,我猜你一定十分熟悉如何在頁面上添加一段javascript代碼,知道如何調用它讓它正常地工作,也許你也能立即想出如何在頁面上使用iframe來嵌套另一個頁面,但真要讓你一下子說出來如何在當前頁面中調用iframe頁面中的javascript代碼或者修改iframe頁面中的某些元素,可能你就得稍微思考一下了~

那麼,請參考如下代碼吧:

<!-- parent.html -->
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <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()" οnclick="callChild()"/>
    <iframe name="myFrame" src="child.html" />
</body>
</html>

<!-- child.html -->
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <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()函數" οnclick="callParent()" />
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章