javascript - 在父级中获取子级iframe框架中的方法,疑问
本文介绍了javascript - 在父级中获取子级iframe框架中的方法,疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
父级代码
<body>
<iframe name="my" src="a.html" border="0"></iframe>
<script>
frames[0].fn1();
</script>
</body>
子级a.html的代码
<script>
var i = 0;
function fn1(){
alert("这里是子窗口");
};
</script>
在父级中调用a.html的方法fn1,在firefox的代码草稿纸中运行ok,但是在源代码中运行报错
TypeError: frames[0].window.fn1 is not a function
frames[0].fn1();
解决方案
最后还是用window的方法进行解决,需要层层地调试,一点不注意都会出错,我写了一个父级与两个兄弟之间相互调用方法的例子,直接使用window方法,更简洁!!!
a.html 如下
<body>
<iframe id="b" src="b.html" frameborder="0"></iframe>
<iframe id="c" src="c.html" frameborder="0"></iframe>
<script>
var b = frames[0];
var c = frames[1];
setTimeout(function (){
console.log(b.i);
},3000);
</script>
</body>
b.html 如下
<body>
<script>
var i = "this is b";
setTimeout(function (){console.log(parent.c.i)},2000);
</script>
</body>
c.html 如下
<body>
<script>
var i = "this is c"
setTimeout(function (){console.log(parent.b.i)},1000);
</script>
</body>
这篇关于javascript - 在父级中获取子级iframe框架中的方法,疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文