繁体   English   中英

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

[英]iframe contentWindow is undefined when use window.frames[name] to access

如果使用以下方式获取contentWindow,则值为undefined

<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

小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM