当值错误时,我试图强制光标返回到之前的输入。 这是我使用的脚本的一个简单版本:
$('input:first-child').blur(function(e){
console.log('here');
$('input:first-child').focus();
});
当我单击另一个元素进行焦点聚焦时,它可以工作,但当我单击 body 或另一个 div 元素时,它不起作用。为什么? 我有一个 jsfiddle 案例:http://jsfiddle.net/leyou/Zabn4/37/
编辑: 我在 Chrome/Win7 上进行了测试,但在 Firefox 上似乎更糟。它完全忽略了 focus()
最佳答案
发生这种情况是因为模糊尚未完成。用一个小的超时来完成它就会起作用:
$('input:first-child').blur(function(e){
setTimeout(function () { $('input:first-child').focus(); }, 20);
});
关于jquery - focus() 不会将光标带入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13346669/