问题描述
如何使html输入始终保持焦点?创建元素时使用 autofocus
属性有助于将焦点集中在页面加载上,但不能将焦点集中在对象上.有什么简单的方法可以使焦点保持在那里?
How can I keep the html input always in focus?Using the autofocus
attribute when creating the element helps to focus at pageload, but it does not keep the focus on the object. Is there any simple way to keep the focus there?
推荐答案
尝试一下:
<!-- On blur event (unfocus) refocus the input -->
<input onblur="this.focus()" autofocus />
<input value="Can't focus on this" />
基本上,您添加一个 blur
事件(每当输入失去焦点时就会发生),而在blur事件中,您将注意力集中在输入上.此外,您还添加了 autofocus
属性以开始关注焦点.
Basically, you add a blur
event (which occurs whenever the input loses focus), and in the blur event, you focus on the input. In addition, you add the autofocus
attribute to start off focused as well.
P.S.从用户界面的角度来看,如果您打算将其用于供实际用户使用的网站,那么这可能不是一个好主意.
P.S. If you are planning to use this for a website for actual people to use, from a user interface perspective, this is probably not a great idea.
这篇关于HTML输入,始终保持焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!