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

前端技术 2023/09/04 JavaScript

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程序设计有所帮助。

本文地址:https://www.stayed.cn/item/10784

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。