如何使视野自动对焦? [英] How do I make a field autofocus?

查看:79
本文介绍了如何使视野自动对焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户打开页面时,我需要将此字段作为焦点.我不知道它是否会发生任何变化,但它位于我从PHP文件加载的模式窗口内.

I need this field to be focused when the user opens the page. I don't know if it changes anything but it's inside a modal window I load from a PHP file.

有简单的方法吗?

推荐答案

JavaScript自动聚焦器(如此处其他答案所述)将起作用,但可能会使某些用户感到烦恼.例如,如果在页面仍在加载时将焦点放在另一个字段上,则JavaScript可能会通过移动焦点并键入错误的字段来帮助"您. ux.stackexchange.com上的这个问题列举了更多缺点.

JavaScript autofocusers (as given in the other answers here) will work but can be annoying for some users. For example, if you focus a different field while a page is still loading, the JavaScript may "help" you by moving the focus and making you type in the wrong field. This question on ux.stackexchange.com enumerates some more drawbacks.

为防止这种情况,HTML5引入了

To prevent this, HTML5 has introduced an autofocus attribute for form elements that means the behaviour will be consistently implemented by the browser itself, and can be turned off for users that find it irritating. Of course, this isn't supported by all browsers yet. For more info, see the section on autofocus fields in Mark Pilgrim's excellent Dive Into HTML5 book.

这篇关于如何使视野自动对焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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