网络编程
位置:首页>> 网络编程>> JavaScript>> js父页面中使用子页面的方法

js父页面中使用子页面的方法

作者:一落叶而知秋  发布时间:2024-04-19 09:50:49 

标签:js,父页面,子页面,iframe

iframe是非常常用的一个html元素,如果在父页面中使用子页面的方法应该怎么写呢,下面就做一下简单的介绍。
一、父页面代码


<html>
<head>
<meta charset=" gb2312">
<title>父页面</title>
<script type="text/javascript">
function parentFunction()
{
alert('function in parent');
}
function callChild()
{
child.window.childFunction();
/*
 child 为iframe的name属性值,
 不能为id,因为在FireFox下id不能获取iframe对象
*/
}
</script>
</head>
<body>
<iframe name="child" src="./child.html" ></iframe>
</body>
</html>

二、iframe中的代码


<html>
<head>
<meta charset="gb2312">
<title>iframe代码</title>
<script type="text/javascript">
function childFunction()
{
alert('function in child');
}
function callParent()
{
parent.parentFunction();
}
</script>
</head>
<body>
</body>
</html>

上面两个代码可以在父页面和子页面对对方的函数进行相互调用,比较简单,不多介绍了。
希望本文所述对大家学习javascript程序设计有所帮助。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com