如何在Javascript中以编程方式从iframe获得父窗口的焦点 [英] How to get focus back for parent window from an iframe programmatically in Javascript

查看:137
本文介绍了如何在Javascript中以编程方式从iframe获得父窗口的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些网站在窗口加载元素上设置 focus 。如果我的网站的iframe,似乎不能够设置侧重于父窗口,编程。这只发生在 IE (9)和 Firefox (19),Opera / Safari / Chrome很好。 p>

Some websites have focus set on an element on window load. If I iframe that website, It seems not able to set focus on parent window, programmatically. This only happens to IE (9) and Firefox (19), Opera/Safari/Chrome are fine.

<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" />

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

Does anyone know a workaround for this problem?

推荐答案

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

Got the answer, now it works on all browsers

<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" />

这篇关于如何在Javascript中以编程方式从iframe获得父窗口的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆