0

我有一个可以从条形码阅读器获取输入值的网站。我需要保持输入字段焦点,直到它获得值。我<input onBlur="this.focus()">使用函数和一些变体,但不起作用。我宁愿不使用 jQuery,但这不是强制性的。

4

1 回答 1

1

What you want to do might be perfectly fine in your specific use case, but please don't do this if it isn't really, absolutely necessary. It is very annoying, if users can't control the focus anymore. Having said that, you could do it like this:

<input onBlur="if (this.value == '') { var field = this; setTimeout(function() { field.focus(); }, 0); }" type="text">

You probably should put this in a separate function.

于 2013-07-19T11:44:13.707 回答