如何访问Iframe的父窗口的方法[Chrome浏览器] [英] How to access a method of parent window of an Iframe [Chrome browser ]
问题描述
我尝试了很多次和很多方法来从 iframe
中调用方法,但尚未成功。请参阅下面的内容,
-
main.html:
-
iframe-1与index.html链接,从中我想调用main.html的方法或想要更改第二个iframe的src。 / b>
main.html
< HTML>
< head> < /头>
< body>
< iframe id =iframe-1src =index.html>< / iframe>
< iframe id =iframe-2>< / iframe>
< / body>
< / html>
index.html
< html> < HEAD>
< script type =text / javascript>
$(document).ready(function(){
//如何访问main.html的方法并更改iframe src
});
< / script>
< / head> <身体GT;
......
< / body> < / HTML>
注意:尝试 parent.methodName()
, window.parent.methodName()
无法使用
@EDIT:IE& MOZILLA但在Chrome上出现错误(无法调用未定义的方法'getElementById')
index.html
< html>
< head>
< script type =text / javascript>
function run(){
window.parent.document.getElementById(iframe-2)。src =/test.html;
}
< / script>
< / head>
< body onload =run();>
< / body>
< / html>
如何?
或者创建main.html中的一个方法,并使用它:
window.parent。 methodname ();
Ron。
ps。 window.parent的方法名强>();在main.html中有一个方法时,完美地适用于我 main.html I tried many times and many ways to call the method from inside the iframe-1 linked with a index.html from where i want to call a method of main.html or want to change the src of second iframe. main.html index.html Note : tried @EDIT : success on IE & MOZILLA but getting error on Chrome ( Cannot call method 'getElementById' of undefined ) index.html How about this? Or create a method inside main.html and access it using:
window.parent.methodname(); Ron. ps. window.parent.methodname(); works perfectly for me when I have a method in main.html main.html
这篇关于如何访问Iframe的父窗口的方法[Chrome浏览器]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< HTML>
< head> < /头>
< script>
函数foo(){
alert(1);
}
< / script>
< body>
< iframe id =iframe-1src =index.html>< / iframe>
< iframe id =iframe-2>< / iframe>
< / body>
< / html>
iframe
while not yet successful to do so. please see below,
main.html :
consisting the two iframe <html>
<head> </head>
<body>
<iframe id="iframe-1" src="index.html"></iframe>
<iframe id="iframe-2" ></iframe>
</body>
</html>
<html> <head>
<script type="text/javascript">
$(document).ready(function(){
// How to access the method of main.html and change the iframe src
});
</script>
</head> <body>
......
</body> </html>
parent.methodName()
, window.parent.methodName()
not working <html>
<head>
<script type="text/javascript">
function run() {
window.parent.document.getElementById("iframe-2").src = "/test.html";
}
</script>
</head>
<body onload="run();">
</body>
</html>
<html>
<head> </head>
<script>
function foo() {
alert(1);
}
</script>
<body>
<iframe id="iframe-1" src="index.html"></iframe>
<iframe id="iframe-2" ></iframe>
</body>
</html>