如何在Javascript中以编程方式从iframe获取父窗口的焦点

use*_*156 2 javascript iframe firefox internet-explorer focus

一些网站focus在窗口加载上设置了一个元素.如果我iframe那个网站,似乎无法以编程方式将焦点设置在父窗口上.这只发生在IE(9)和Firefox(19),Opera/Safari/Chrome都没问题.

<script>
window.onload = function() {
    setTimeout(function() {
        // this does not focus #foo
        document.getElementById("foo").focus();
    // or more seconds that enough to see the iframe focus
    }, 3000);
};
</script>
<input id="foo" type="text" value="foo" /><br />
<iframe width="800" height="500" src="http://www.bing.com" />
Run Code Online (Sandbox Code Playgroud)

有谁知道这个问题的解决方法?

use*_*156 5

得到答案,现在它适用于所有浏览器

<script>
window.onload = function() {
    // blur the iframe
    document.getElementById("bing").blur();
    // set focus on #foo
    document.getElementById("foo").focus();
    // when iframe tries to focus, focus #foo
    document.getElementById("foo").onblur = function() {
        this.focus();
    };
};
</script>
<input id="foo" type="text" value="foo" /><br />
<iframe id="bing" width="800" height="500" src="http://wwww.bing.com" />
Run Code Online (Sandbox Code Playgroud)