在 Chrome 中将焦点设置在 iframe 上
我在 Chrome 中有一个带有 designMode='on'
的 iframe (id: 'chat'
)。
在 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);
}
我已经尝试了许多其他问题的解决方案,但似乎没有一个有效。
I have an iframe (id: 'chat'
) with designMode='on'
in Chrome.
On Enter keypress event I call the function send()
, which takes the iframe contents and writes it to a socket. My problem is that when clearing the iframe, I lose focus.
How to do I set the focus so I can continue to type text in the 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);
}
I've tried many of the solutions on other questions, but none seems to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
诀窍是首先将焦点设置在主体上,然后创建一个
Range
。The trick is to first set focus on the body and then create a
Range
.这个问题已在这里得到解答
基本上,如果您不刷新 iframe,您可以使用:
请注意,我正在获取底层 iframe DOM 对象。
This question has been answered here
Basically, if you are not refreshing the iframe you could use:
Note that I'm grabbing the underlying iframe DOM object.
我已经尝试过以下解决方案,它适用于所有浏览器(IE/Chrome/Firefox)
上下文:我想始终关注 iframe。
希望对有需要的人有帮助...
I have tried below solution it works in all browser (IE/Chrome/Firefox)
Context: I want to focus the iframe all the time.
Hope it helps, if any one in need...
我用 Chrome 测试了这个解决方案。我最初将其发布在将焦点设置为 iframe 内容。
下面是使用 jQuery 创建 iframe 的代码,将其附加到文档中,轮询直到加载,然后将其聚焦。这比设置任意超时要好,该超时可能会也可能不会起作用,具体取决于 iframe 加载所需的时间。
用于检测 iframe 何时加载的代码改编自 Biranchi 对 如何检查 iframe 是否已加载或是否有内容?
I tested this solution with Chrome. I originally posted it in Setting focus to iframe contents.
Here is code to create an iframe using jQuery, append it to the document, poll it until it is loaded, then focus it. This is better than setting an arbitrary timeout which may or may not work depending on how long the iframe takes to load.
The code for detecting when the iframe is loaded was adapted from Biranchi's answer to How to check if iframe is loaded or it has a content?