我尝试了很多次和许多方法从iframe内部调用该方法,但还没有成功。请看下面,
main.html :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()不工作
@编辑:在IE和MOZILLA上成功,但在Chrome上出现错误(不能调用未定义的getElementById方法)
发布于 2013-07-12 10:13:06
你应该试试
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文档
发布于 2013-07-12 10:18:20
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中创建一个方法并使用:main.html访问它:
罗恩。
ps。当我在window.parent.methodname();中有一个方法时,main.html非常适合我。
main.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>发布于 2013-07-12 10:18:43
我要保持这个简短。
因为您期待相同的文档窗口共享上的iframes/框架。要访问在一个文档中定义的变量,在另一个document.You中必须使用document.importNode(与其他文档一样,布尔)方法(按照DOM 2 )。
iframe=document.getElementsTagName("iframe")[0];documentI(这里有原始变量/节点)-
OriginalNode=iframe.contentWindow.document.getElementsByTagName(//Tag name of Node//)documentII(将在此克隆的节点)- iframe.contentWindow.document.importNode(OriginalNode,True)
节点可以通过简单的DOM方法在任何iframe中创建任何方法、属性或对象。这是可行的
https://stackoverflow.com/questions/17612514
复制相似问题