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 type="button" value="调用child.html中的函数say()"/>
<iframe src="child.html"></iframe>
</body>
</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 type="button" value="调用parent.html中的say()函数"/>
</body>
</html>
新闻热点
疑难解答
图片精选