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

编程学习 2021-07-04 21:04www.dzhlxh.cn编程入门
这篇文章主要向大家介绍了js父页面中使用子页面的方法,即js父页面使用iframe中的函数,感兴趣的朋友可以参考一下

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

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

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板