iframe ??? ?? ?? ?????.
? ??: ????? HTML ??? ? ? ?? ? ??? ?????. ??? ??? ?? ??? ???? ???? ??(?, ?? ??? ??? ?? iframe ??? ???) ????? ?? ??? ?? ? ??? ?? ?? ? iframe? ?? ?? ? ??? ????. ?? ?? ?? ?.
contentWindow: ??? iframe ?? iframe? ??? ? ??? ?????.
Demo1
?? ??? fu.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>父頁面</title>
</head>
<body>
<input type=button value="調(diào)用子頁面中的函數(shù)childSay函數(shù)" 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>
<head>
<meta charset="utf-8">
<title>子頁面</title>
</head>
<body>
<input type=button value="調(diào)用父頁面中的parentSay()函數(shù)" onclick="callParent()">
<script type="text/javascript">
function childSay()
{
alert("我是子頁面的say方法");
}
function callParent() {
parent.parentSay();
}
</script>
</body>
</html>
Demo2
?? ??? iframe1.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>父頁面與子頁面之間的調(diào)用</title>
</head>
<body>
<iframe src="http://localhost/iframe/iframe3.html" id="iframe3"></iframe>
<iframe src="http://localhost/iframe/iframe2.html" id="iframe2"></iframe>
<div>我是父頁面</div>
<script type="text/javascript">
var iframe2=document.getElementById('iframe2');
iframe2.onload=function(){//父頁面調(diào)用子頁面中的方法
iframe2.contentWindow.b();
};
function test2() {
console.log("我是父頁面中的方法,在子頁面中調(diào)用的");
return iframe2;
}
</script>
</body>
</html>
?? ??? iframe2.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>子頁面</title>
</head>
<body>
<div id="test">aaa</div>
<div>子頁面</div>
<script type="text/javascript">
//子頁面iframe2.html調(diào)用父頁面iframe1.html的函數(shù):
parent.test2();
function b(){
console.log("我是子頁面iframe2");
}
function c() {
console.log("iframe3頁面調(diào)用iframe2頁面");
}
</script>
</body>
</html>
?? ??? iframe3.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>iframe3</title>
</head>
<body>
<script type="text/javascript">
var iframe2=parent.test2();
iframe2.contentWindow.c();//iframe3調(diào)用iframe2中的方法
</script>
</body>
</html>
Demo2? ?? ??? ?? ?? ??? ?????.