日期:2014-05-16  浏览次数:20328 次

在iframe中调用js父页面和子页面方法

在iframe中调用父页面和子页面js方法:

今天朋友问我:a页面用iframe引入b页面,而此时要在a页面怎么调用iframe所引入b页面里的方法,很熟悉,但就是忘记了,后来百度下,才晓得,主要通过window.frames["name属性"].方法名(),也就是iframe中的name属性,b页面调用a页面中的方法略过看代码吧

index.html

<html>
<head>
<script type="text/javascript">
function ff(){
alert(">>this is index's js function");
}
</script>
</head>
<body>
<div style="background: lightblue;">
<input type="button" value="index" onclick="ff();" />
<input type="button" value="inner" onclick='window.frames["childPage"].ff();' />
</div>
<iframe id="" name="childPage" src="inner.html" width="100%" frameborder="0"></iframe>
</body>
</html>

inner.html

?

<html>
<head>
<script type="text/javascript">
function ff(){
alert(">>sdfdsdfdsfdsfsdfthis is inner page's js function");
}
</script>
</head>
<body>
<div>
<input type="button" value="index" onclick='parent.window.ff();' />
<input type="button" value="inner" onclick="ff();" />
</div>
</body>
</html>

?

?

子页面调用父页面的JS方法:window.parent.方法名();