Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Chrome中设置iframe的焦点_Javascript_Jquery_Google Chrome_Iframe_Designmode - Fatal编程技术网

Javascript 在Chrome中设置iframe的焦点

Javascript 在Chrome中设置iframe的焦点,javascript,jquery,google-chrome,iframe,designmode,Javascript,Jquery,Google Chrome,Iframe,Designmode,我有一个iframe(id:'chat'),在Chrome中带有designMode='on' 在enterkeypress事件中,我调用函数send(),它获取iframe内容并将其写入套接字。我的问题是,当清除iframe时,我会失去焦点 如何设置焦点,以便在iframe中继续键入文本 function send(){ var $iframe = $('#chat'); var text = $iframe.contents().text() + "\n"; s

我有一个iframe(
id:'chat'
),在Chrome中带有
designMode='on'

在enterkeypress事件中,我调用函数
send()
,它获取iframe内容并将其写入套接字。我的问题是,当清除iframe时,我会失去焦点

如何设置焦点,以便在iframe中继续键入文本

function send(){
    var $iframe = $('#chat');
    var text = $iframe.contents().text() + "\n";  

    socket.send(text);

    // When this is done, the focus is lost
    // If commented, the focus will not be lost
    $iframe.contents().find('body').empty();

    // My different failed attempts to regain the focus
    //$iframe.focus();
    //$iframe.contents().focus();
    //$iframe.contents().find('body').focus();
    //$iframe.contents().find('body').parent().focus();
    //$iframe[0].contentWindow.focus();

    // I've also tried all the above with timers
    //setTimeout( function(){ $('#chat').contents().focus(); }, 100);
}
我在其他问题上尝试了许多解决方案,但似乎都不管用。

这个问题已经解决了

基本上,如果不刷新iframe,可以使用:

$iframe[0].contentWindow.focus();

请注意,我正在抓取底层的iframe DOM对象。

诀窍是首先将焦点设置在主体上,然后创建一个
范围

var win = iframe.contentWindow;
var range = win.document.createRange();
range.setStart(win.document.body, 0);
range.setEnd(win.document.body, 0);
win.document.body.focus();
win.getSelection().addRange(range);

我用铬测试了这个溶液。我最初把它贴在了

下面是使用jQuery创建iframe的代码,将其附加到文档中,轮询它直到加载它,然后聚焦它。这比设置任意超时要好,该超时可能有效,也可能无效,具体取决于iframe加载所需的时间

var jqueryIframe = $('<iframe>', {
    src: "http://example.com"
}),
focusWhenReady = function(){
    var iframe = jqueryIframe[0],
    doc = iframe.contentDocument || iframe.contentWindow.document;
    if (doc.readyState == "complete") {
        iframe.contentWindow.focus();
    } else {
        setTimeout(focusWhenReady, 100)
    }
}
$(document).append(jqueryIframe);
setTimeout(focusWhenReady, 10);
var jqueryframe=$(“”{
src:“http://example.com"
}),
focusWhenReady=函数(){
var iframe=jqueryframe[0],
doc=iframe.contentDocument | | iframe.contentWindow.document;
如果(doc.readyState==“完成”){
iframe.contentWindow.focus();
}否则{
设置超时(焦点WhenReady,100)
}
}
$(文档).append(jqueryframe);
设置超时(焦点时间,10);

用于检测何时加载iframe的代码根据的回答改编为

我已经尝试了以下解决方案它在所有浏览器(IE/Chrome/Firefox)中都能工作

上下文:我想一直关注iframe

    function IFocus() {
        var iframe = $("#iframeId")[0];
        iframe.contentWindow.focus();
    };
    window.setInterval(IFocus, 300);

希望它能帮上忙,如果有人需要的话…

我也试过了。不幸的是,我仍然无法恢复焦点。(我编辑了问题并添加了你的建议)如果你在setTimeout中这样做,给iframe一点时间开始充气,会怎么样?显然这就是诀窍!它起作用了。这让我很开心。。现在,我只有一个新的线索从事件仍然挥之不去。但至少我有我的焦点!