我尝试了很多次和很多方法来从 iframe
内部调用该方法,但尚未成功。请看下面,
main.html :
由两个 iframe 组成iframe-1 与 index.html 链接,我想从其中调用 main.html 的方法或想要更改第二个 iframe 的 src。
main.html
<html>
<head> </head>
<body>
<iframe id="iframe-1" src="index.html"></iframe>
<iframe id="iframe-2" ></iframe>
</body>
</html>
index.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()
不起作用
@EDIT:在 IE 和 MOZILLA 上成功,但在 Chrome 上出现错误(无法调用未定义的“getElementById”方法)
最佳答案
你应该尝试使用
document.getElementById("iframe-1").contentWindow.func();
或
var $f = $("#myIFrame");
var fd = $f[0].document || $f[0].contentWindow.document; // document of iframe
fd.MyFunction(); // run function
关于javascript - 如何访问Iframe父窗口的方法[Chrome浏览器],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17612514/