保持对输入标签的关注 [英] Maintain focus on an input tag

查看:87
本文介绍了保持对输入标签的关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < input onblur =this。 focus()/> 

但似乎没有用。

解决方案

如果我们在 blur >上调用 .focus()事件,它会恢复焦点,但实际上不会有文本光标。为了处理这个问题,我们必须让元素失去焦点,然后在几毫秒内返回它。我们可以为此使用 setTimeout()
$ b

  $('# (){
var blurEl = $(this);
setTimeout(function(){
blurEl.focus()
} ,10);
});

这里是工作示例。注意 - 输入后不能留下文本字段=)



编辑我使用jQuery,但可以轻松完成它。

EDIT2 下面是纯JS版本小提琴

 < input type =textid =elemID/> 

< script type =text / javascript>
document.getElementById('elemID')。onblur = function(event){
var blurEl = this;
setTimeout(function(){
blurEl.focus()
},10);
};
< / script>


I'm trying to keep focus on an input element with this code:

<input onblur="this.focus()" />

But it doesn't seem to work.

解决方案

If we just call .focus() right on blur event, it will restore focus, but actually there will be no text cursor. To handle this we have to let element to lose focus and then return it in few milliseconds. We can use setTimeout() for this.

$('#inp').on('blur',function () { 
    var blurEl = $(this); 
    setTimeout(function() {
        blurEl.focus()
    }, 10);
});

Here's working example. Be careful - you can't leave text field after you enter it =)

EDIT I used jQuery, but it can be easily done without it.
EDIT2 Here's pure JS version fiddle

<input type="text" id="elemID" />

<script type="text/javascript">
    document.getElementById('elemID').onblur = function (event) { 
        var blurEl = this; 
        setTimeout(function() {
            blurEl.focus()
        }, 10);
    };
</script>

这篇关于保持对输入标签的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆