核心代码:
//调用子页面方法:
document.getElementById("content").contentWindow.childtest();
//调用父页面方法:
parent.window.sayInParent();
父页面parent.html
<html>
<head>
<script type="text/javascript">
function callChild() {//调用子页面js方法
document.getElementById("content").contentWindow.childtest();
}
function sayInParent(){
alert("sayInParent");
}
</script>
</head>
<body>
<input type="button" value="调用子页面的方法" onclick="callChild()"/> <br/>
<iframe name="myFrame" src="child.html"></iframe>
</body>
</html>
子页面child.html
<html>
<head>
<script language="javascript" type="text/javascript">
function childtest() {
alert("这是子页面的方法!");
}
function callParent() {//调用父页面js方法
parent.window.sayInParent();
}
</script>
</head>
<body>
<input type="button" value="调用父页面的方法" onclick="callParent()"/>
</body>
</html>