gpt4 book ai didi

javascript - 在 IE 中访问嵌套 iframe 中的 javascript 函数

转载 作者:搜寻专家 更新时间:2023-11-01 05:03:05 25 4
gpt4 key购买 nike

我目前有一个页面结构,该页面结构由一个包含 iframe(iframe0) 的页面(父级)组成,在该 iframe 中我有另一个 iframe(iframe1)。在 iframe1 中,我有一个 javascript 函数,我试图从 Parent 调用它。在 Firefox/Chrome/Safari 中,我可以使用以下代码调用此函数:

 frames["iframe0"]["iframe1"].functionName();

但是,在 Internet Explorer 中,上述代码不起作用,它返回错误“对象不支持此属性或方法”。我尝试了一些其他方法来访问该方法,但它们都返回相同的错误。

 window.frames.iframe0[iframe1].functionName();
window.iframe0.iframe1.functionName();
window.frames.iframe0.frames.iframe1.functionName();

我什至尝试在 iframe0 中调用一个调用 iframe1 中的函数的函数,但它甚至不起作用。

有人知道如何访问嵌套在 2 层深的 iframe 中的 javascript 函数吗?

谢谢。

更新:进一步调查问题后,我发现我正在处理的问题与我所问的无关。 ylebre 在下面给出的答案回答了我提出的问题,并将我标记为答案。我可能会开始另一个问题,更详细地描述我的问题。

最佳答案

我提供了一个使用 3 个 HTML 文件的示例。最外层是 test.html,它有一个包含 iframe1.html 的 iframe。反过来,iframe1.html 包含一个包含 iframe2.html 的 iframe。我希望这是您想要的那种设置。

基本上,您可以使用 iframe.contentWindow.myfunc(); 调用 iframe 函数;

然后使用 contentWindow.document 可以访问二级 iframe。

示例函数“doit()”调用父级、第一个 iframe 和第二个 iframe 中的函数。

希望这对您有所帮助!

<!------- test.html -------->
<html>
<head>
<script type="text/javascript">
function parent_function() {
alert('parent');
}
function doit() {
parent_function();
document.getElementsByTagName('iframe')[0].contentWindow.iframe1_function();
document.getElementsByTagName('iframe')[0].contentWindow.document.getElementsByTagName('iframe')[0].contentWindow.iframe2_function();
}
</script>
</head>
<body>
main
<a href="javascript:doit();">do it</a>
<iframe src='iframe1.html'>
</body>
</html>

<!------- iframe1.html -------->
<html>
<head>
<script type="text/javascript">
function iframe1_function() {
alert('iframe1');
}
</script>
</head>
<body>
frame1
<iframe src='iframe2.html'>
</body>
</html>

<!------- iframe2.html -------->
<html>
<head>
<script type="text/javascript">
function iframe2_function() {
alert('iframe2');
}
</script>
</head>
<body>
frame2
</body>
</html>

关于javascript - 在 IE 中访问嵌套 iframe 中的 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/921387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com