gpt4 book ai didi

javascript - 使用 window.frames[name] 访问时 iframe contentWindow 未定义

转载 作者:太空狗 更新时间:2023-10-29 13:10:12 26 4
gpt4 key购买 nike

如果使用下面的方式获取contentWindow,则该值未定义

<html>
<head>
<title>iframe test</title>
</head>
<body>
<iframe id="frame1" src="frame1.html" name="frame1"></iframe>
<script>
document.body.onload = function() {
console.info("index loaded");
var frame1 = window.frames["frame1"];
console.info(frame1.contentWindow);
}
</script>
</body>
</html>

如果像下面这样使用其他方式,它工作正常:

var frame1 = document.getElementById("frame1");
console.info(frame1.contentWindow);

我在 FF 29.0.1、chrome 34、IE11 上测试过,它们都以相同的方式工作。

所以我有两个问题:

  1. 为什么第一种方式获取不到contentWindow的值
  2. iframe.contentWindow 是否兼容所有浏览器?

最佳答案

window.frames["frame1"];

contentWindow,它有一个命名的窗口,在你的例子中它和

是一样的
document.getElementById("frame1").contentWindow

FIDDLE

关于javascript - 使用 window.frames[name] 访问时 iframe contentWindow 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23711448/

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