2

一部の Web サイトではfocus、ウィンドウの読み込み時に要素が設定されています。そのWebサイトをiframeすると、プログラムで親ウィンドウにフォーカスを設定できないようです。IEこれは(9) と(19) にのみ発生しFirefox、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" />

この問題の回避策を知っている人はいますか?

4

2 に答える 2

7

答えを得て、すべてのブラウザで動作するようになりました

<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" />
于 2013-04-03T17:17:56.457 に答える
1

私がこれを正しく読んだ場合、フォーカスが親ウィンドウから iframe 内の要素に移動するのを防ぐ必要があります。

次のように、iframe 要素で「onfocus」トリガーを使用して、フォーカスをページに戻します。

<iframe width="800" height="500" src="http://www.bing.com" onfocus="document.getElementById('foo').focus();"/>

iframe が最初のフォーカス onload を盗むのを防ぎたいだけで、ユーザーが後でフォーカスを移動できるようにしたい場合は、次のようにステータス変数を使用します。

<script type="text/javascript">
var initialFocus = false;
</script>

<iframe width="800" height="500" src="http://www.bing.com" onfocus="if (initialFocus==false) { document.getElementById('foo').focus(); initialFocus = true; }"/>

ご不明な点がございましたら、お気軽にお問い合わせください。

于 2013-04-03T16:31:40.420 に答える