我有一个文本字段,无论如何我都想保持焦点。
问题是,我的页面加载了一个 iframe(跨域),其中的内容窃取了焦点。此外,当 iframe 窃取焦点时,用户可能会在我的文本字段中键入内容,从而导致用户改为在 iframe 的文本字段中键入内容。
我尝试向 .focus()
事件发送垃圾邮件,希望如果它变得模糊,它会立即重新聚焦。看起来这可行,但发生的情况是焦点切换回来的速度不够快——用户仍然能够在 iframe 的聚焦字段中输入内容。
有没有办法完全防止模糊发生?或者有一种更快的方法将焦点切换回我的文本字段?
这是我尝试过的方法,但并不完全有效:
//get the textbox DOM element to focus.
var my_textbox = $("#my_textbox").get(0);
//pretty straight-forward -- focus it.
var setFocus = function(){
my_textbox.focus();
}
//every 10ms refocus our textbox.
//apparently, 10ms is not fast enough, as some typing
//still occurs in the iframe's textfield.
setInterval(setFocus, 10);
注意:我昨天做了一个类似的问题,但我认为这个问题找到了问题的根源。希望一切顺利。
最佳答案
你试过这个吗?
//get the textbox DOM element to focus.
var my_textbox = $("#my_textbox").get(0);
my_textbox.onblur = function(){
my_textbox.focus();
};
仍然感觉很困惑,但我认为这就是你想要的。
关于javascript - 防止视野模糊的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3847105/