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

jas*_*son 10 html javascript iframe

如果使用以下方式获取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>
Run Code Online (Sandbox Code Playgroud)

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

var frame1 = document.getElementById("frame1");
console.info(frame1.contentWindow);
Run Code Online (Sandbox Code Playgroud)

我测试了FF 29.0.1,chrome 34,IE11,它们都以相同的方式工作.

所以我有两个问题:

  1. 为什么第一种方式无法获得contentWindow值
  2. iframe.contentWindow在所有浏览器中都兼容吗?

ade*_*neo 14

window.frames["frame1"];
Run Code Online (Sandbox Code Playgroud)

contentWindow,它得到一个命名的窗口,在你的情况下,它是相同的东西

document.getElementById("frame1").contentWindow
Run Code Online (Sandbox Code Playgroud)

小提琴