【问题标题】:how do you focus on an iframe which window contains "window.onclick"您如何专注于窗口包含“window.onclick”的 iframe
【发布时间】:2022-01-14 15:13:40
【问题描述】:

我想在我的网站上放置一个 html 画布游戏。问题是窗口包含window.onclick,这听起来不是什么大问题,但它有点烦人,每次加载页面时都必须点击iframe。所以我想为页面自动聚焦在框架上而无需点击。

问题出在document.querySelector('iframe').focus() 不起作用的地方。它确实让观众将注意力集中在 iframe 上,但 框架窗口没有任何声音。我认为这是因为它专注于 iframe,但它没有专注于 iframe 中的窗口,如果这有任何意义的话。另一方面,窗口确实有一个脚本可以专注于窗口window.onclick = function () { window.focus(); }。但我有点希望它像 document.querySelector('iframe').onfocus = function () { window.focus(); } 那样由于显而易见的原因不起作用。

我尝试了一些方法,但都没有奏效。

<iframe src="https://v6p9d9t4.ssl.hwcdn.net/html/4423984/index.html" width="800" height="600" scrolling="no" onload="this.focus()"></iframe>

<iframe src="https://v6p9d9t4.ssl.hwcdn.net/html/4423984/index.html" width="400" height="300" scrolling="no" onload="this.click()"></iframe>

<iframe src="https://v6p9d9t4.ssl.hwcdn.net/html/4423984/index.html" width="400" height="300" scrolling="no" onfocus="this.click()" onload="this.focus()"></iframe>

<iframe src="https://v6p9d9t4.ssl.hwcdn.net/html/4423984/index.html" width="800" height="600" scrolling="no" style="border: 2px solid purple;" onload="this.contentWindow.focus()"></iframe>

所以我认为窗口不理解click() 函数。

我想要的是让这个脚本返回 true

    function testAudio(){playTestAudio();return _yb1&&g_WebAudioContext&&g_WebAudioContext.state!==_fa1._ga1}function playTestAudio(){if(_yb1||_zb1)return;_zb1=true;var _lb1=new Audio(_X91);_lb1.controls=false;_lb1.autoplay=true;_lb1.preload="none";document.body.appendChild(_lb1);try{var _Bb1=_lb1.play();if(_Bb1!==undefined){_Bb1.then(function(){debug("WebAudio autoplay test passed.");_yb1=true;_zb1=false;_Cb1();document.body.removeChild(_lb1)}).catch(function(error){console.log("WebAudio autoplay test failed: ",error);document.body.removeChild(_lb1);
_zb1=false;_Db1()})}else {console.log("WebAudio autoplay test failed: Playback promise invalid.");document.body.removeChild(_lb1);_zb1=false;_Db1()}}catch(_wC){debug("WebAudio autoplay test failed with exception: "+_wC);document.body.removeChild(_lb1);_zb1=false;_Db1()}}

如果有对焦框窗的方法,请告诉我。如果有任何 javascriptjquery,它可以在单独的 &lt;script&gt; 标记中,但我希望它是一个较短的脚本。不会太长,仅此而已。

【问题讨论】:

    标签: javascript html jquery iframe


    【解决方案1】:

    要访问 iframe 内容,您需要使用 contentWindow.focus() 方法。

        $(document).ready(function(){
            var iframe = $("[ID OF IFRAME]")[0];
             iframe.contentWindow.focus();
         });
    

    或者如果您希望它内联在 iframe 标记中,您可以添加以下内容。

    onload="this.contentWindow.focus()"
    

    【讨论】:

    • 框架还是没有声音。
    • 您可以尝试使用以下内容吗:$(document).ready(function(){ $("#iframe").click(); });
    • 我想要的是让我在游戏中找到的脚本返回true。
    猜你喜欢
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2011-04-21
    • 2014-12-26
    相关资源
    最近更新 更多