关于 iframe 父窗口和子窗口相互的调用方法,我这里做个总结,方便以后用到。
父窗口调用iframe子窗口方法
- HTML语法:<iframe name="myFrame" src="child.html"></iframe>
- 父窗口调用子窗口:myFrame.window.functionName();
- 子窗品调用父窗口:parent.functionName();
- 简单地说,也就是在子窗口中调用的变量或函数前加个parent.就行。
下面是iframe父子窗口整在一起的例子:
<html>
<head>
<script type="text/javascript">
function say1() {
alert("parent.html------>I'm at parent.html");
}
function callChild() {
//document.frames("myFrame").f1();
myFrame.window.say();
}
// :www.xttblog.com
function say2(){
alert("child.html--->I'm at child.html");
}
function callParent() {
parent.say();
}
</script>
</head>
<body>
<input type=button value="调用child.html中的函数say1()" onclick="callChild()">
<iframe name="myFrame" src="child.html"></iframe>
<input type=button value="调用parent.html中的say2()函数" onclick="callParent()">
</body>
</html>
iframe 父窗口和子窗口相互的调用方法
IE中使用方法:
-
父窗口调用子窗口:iframe_ID.iframe_document_object.object_attribute = attribute_value
例子:onClick="iframe_text.myH1.innerText='http://www.xttblog.com';" -
子窗口调用父窗口:parent.parent_document_object.object_attribute = attribute_value
例子:onclick="parent.myH1.innerText='http://www.xttblog.com';"
Firefox中使用方法:
上面在IE下没有问题,但在firefox下不正常。在firefox下,应该是如下调用方法。
-
父窗口调用子窗口:window.frames[“iframe_ID”].document.getElementById("iframe_document_object").object_attribute = attribute_value
例: window.frames[“iframe_text”].document.getElementById("myH1").innerHTML= "http://www.codedq.net"; -
子窗口调用父窗口:parent.document.getElementById("parent_document_object").object_attribute = attribute_value
例: parent.document.getElementById("myH1").innerHTML = "http://www.codedq.net";
时间关系,相关例子就不在贴了。

: » iframe父子窗口互相调用总结
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/251320.html