框架中页面之间通信

dom07.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function test(){
alert("this is a function in parent")
}
function testCall(){
//第一种 获取iframe的方式 通过数组下标获取
//console.log(window.frames[0]);
//当有名字的时候 可以通过对象的方式去得到
//console.log(window.frames.iframe1);

//通过window直接获取 window <==> window.frames
//console.log(window[0]);
//console.log(window['iframe1']);
//console.log(window.iframe1);
var a = window[0].iframe1();
alert(a);
}
var win;
function testOpen(){
win = window.open("iframe2.html","","width=300px,height=400px");
//console.log(a);

}

function testCall1(){
win.iframe2();
}
</script>
<style>
iframe{
width: 400px;
height: 300px;
border: 1px solid;
}
</style>
</head>
<body>
<input type="button" value="click" οnclick="testCall()">
<input type="button" value="open" οnclick="testOpen()">
<input type="button" value="call" οnclick="testCall1()">
<hr>
<iframe src="iframe1.html" name="iframe1" frameborder="0"></iframe>
<iframe src="iframe2.html" name="iframe2" frameborder="0"></iframe>
</body>
</html>


iframe1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
function iframe1(){
alert("iframe1");
return 123;
}

function testCall(){
//console.log(window);
window.parent.test();
}
</script>

</head>
<body>
<input type="button" value="click" οnclick="testCall()">
<h1>this is a iframe 1</h1>
</body>
</html>


iframe2.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
function iframe2(){
alert("iframe2");
}

function testCall(){
window.opener.test();
}
</script>
</head>
<body>
<input type="button" value="click" οnclick="testCall()">
<h1>this is a iframe 2</h1>

</body>
</html>

转载于:https://www.cnblogs.com/hwgok/p/5739818.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值