首页 技术 正文
技术 2022年11月15日
0 收藏 604 点赞 2,370 浏览 2036 个字

iframe元素就是文档中的文档。

window对象: 浏览器会在其打开一个HTML文档时创建一个对应的window对象。但是,如果一个文档定义了一个或者多个框架(即:包含一个或者多个frame或者iframe标签),浏览器就会为原始文档创建一个window对象,再为每个iframe创建额外的window对象,这些额外的window对象是原始窗口的子窗口。
contentWindow: 是指指定的iframe或者iframe所在的window对象

Demo1

父页面fu.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>父页面</title>
</head>
<body>
<input type=button value="调用子页面中的函数childSay函数" onclick="callChild()">
<iframe id="myFrame" src="zi.html"></iframe>
<script type="text/javascript">
function parentSay() {
alert("我是父页面中的方法");
}
function callChild()
{
document.getElementById("myFrame").contentWindow.childSay();
}
</script>
</body>
</html>

子页面zi.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>子页面</title>
</head>
<body>
<input type=button value="调用父页面中的parentSay()函数" onclick="callParent()">
<script type="text/javascript">
function childSay()
{
alert("我是子页面的say方法");
}
function callParent() {
parent.parentSay();
}
</script>
</body>
</html>

Demo2

父页面iframe1.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>父页面与子页面之间的调用</title>
</head>
<body>
<iframe src="http://localhost/iframe/iframe3.html" id="iframe3"></iframe>
<iframe src="http://localhost/iframe/iframe2.html" id="iframe2"></iframe>
<div class="iframe1">我是父页面</div>
<script type="text/javascript">
var iframe2=document.getElementById('iframe2');
iframe2.onload=function(){//父页面调用子页面中的方法
iframe2.contentWindow.b();
};
function test2() {
console.log("我是父页面中的方法,在子页面中调用的");
return iframe2;
}
</script>
</body>
</html>

子页面iframe2.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>子页面</title>
</head>
<body>
<div id="test">aaa</div>
<div class="iframe2">子页面</div>
<script type="text/javascript">
//子页面iframe2.html调用父页面iframe1.html的函数:
parent.test2();
function b(){
console.log("我是子页面iframe2");
}
function c() {
console.log("iframe3页面调用iframe2页面");
}
</script>
</body>
</html>

子页面iframe3.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>iframe3</title>
</head>
<body>
<script type="text/javascript">
var iframe2=parent.test2();
iframe2.contentWindow.c();//iframe3调用iframe2中的方法
</script>
</body>
</html>

Demo2也实现了子页面与子页面之间相互调用。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,500
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,914
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,747
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,504
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,142
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,306