我有一个 iframe (id: 'chat'
) with designMode='on'
在 Chrome 中。
在 Enter 按键事件上我调用该函数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);
}
我在其他问题上尝试了许多解决方案,但似乎都不起作用。
诀窍是首先将焦点集中在身体上,然后创建一个Range
.
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)