我在 Chrome 中有一个 iframe ( id: 'chat'
) 。designMode='on'
在 Enter keypress 事件中,我调用该函数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);
}
我已经尝试了许多其他问题的解决方案,但似乎没有一个有效。