Javascript 如何访问Iframe[Chrome浏览器]的父窗口的方法
我尝试了很多次,很多种方法从Javascript 如何访问Iframe[Chrome浏览器]的父窗口的方法,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我尝试了很多次,很多种方法从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"></i
iframe
内部调用该方法,但都没有成功。请看下面
main.html:
由两个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>
<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>
index.html
<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>
<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>
$(文档).ready(函数(){
//如何访问main.html方法并更改iframe src
});
......
注意:已尝试parent.methodName()
,window.parent.methodName()
无效
@编辑:在IE和MOZILLA上成功,但在Chrome上出错(无法调用未定义的“getElementById”方法)您应该尝试使用
document.getElementById("iframe-1").contentWindow.func();
或
index.html
<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>
<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>
函数运行(){
window.parent.document.getElementById(“iframe-2”).src=“/test.html”;
}
这个怎么样
或者在main.html内创建一个方法,并使用以下方法访问它:
window.parent.methodname()
罗恩
ps.window.parent.methodname();当我在main.html中有一个方法时,它非常适合我
main.html
<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>
<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>
函数foo(){
警报(1);
}
我的话很简短
正如您期待同一文档[窗口共享]上的iframes/frames一样。要在另一个文档中访问在一个文档中定义的变量,必须根据DOM 2使用document.importNode(其他文档中的原始节点,布尔值)方法。
对JavaScript代码执行类似操作
iframe=document.getElementsTagName("iframe")[0];
文档i(此处显示的原始变量/节点)——
documentII(此处要克隆的节点)iframe.contentWindow.document.importNode(OriginalNode,True)
可以通过简单的DOM方法在任何iframe中创建任何方法、属性或对象的节点。
这将起作用谢谢@Baadshah,但我的问题是要么访问主窗口方法,要么更改兄弟iframe的
src
。我想从index.html
访问,而不是像你描述的从main.html访问iframe的方法那样的“main.html”,这不是我问题的解决方案。我稍微澄清了我的帖子,因为我认为这正是你想要的。从index.html.html内部访问方法或IFRAME(在main.html中声明)。是的,它在Mozilla和IE上工作,而chrome给出了错误。我一直只在chrome上检查,我也应该早点在IE和mozilla上检查。对Chrome有什么想法吗?你到底在哪里使用getElementById?更具体地说,在哪个对象上?如果我在Chrome中测试我的2个HTML页面,它会正常工作。window.parent.document.getElementById应该可以工作。您能在出现错误的地方发布代码吗?或者更好的是,两个HTML页面都包括Javascript?@Ron的解决方案在mozilla和IE上运行,而我在Chrome上遇到错误