哪些HTML元素可以获得焦点? [英] Which HTML elements can receive focus?
问题描述
我正在寻找一个允许重点关注的HTML元素的明确列表,也就是说,当调用 focus()
时,哪些元素将被放到焦点上。 ?
我正在写一个jQuery扩展,它可以在可以引起焦点的元素上工作。我希望这个问题的答案会让我要具体说明我定位的元素。
有没有一个明确的列表,取决于浏览器。我们唯一的标准是 DOM Level 2 HTML ,根据该具有一个焦点()
方法的唯一元件是HTMLInputElement,HTMLSelectElement,HTMLTextAreaElement和HTMLAnchorElement。这尤其省略HTMLButtonElement和HTMLAreaElement。
今天的浏览器定义焦点()
上的HTMLElement,但元件不会实际上取得焦点,除非它是之一:
- HTMLAnchorElement / HTMLAreaElement带有href
- HTMLInputElement / HTMLSelectElement / HTMLTextAreaElement / HTMLButtonElement但与
禁用
(如果你尝试IE确实给你一个错误),以及文件上传出于安全考虑不寻常的行为 - HTMLIFrameElement(尽管聚焦它没有任何用处)。其他嵌入元素,也许我没有测试过它们。
- 任何元素都带有
tabindex
- HTMLAnchorElement/HTMLAreaElement with an href
- HTMLInputElement/HTMLSelectElement/HTMLTextAreaElement/HTMLButtonElement but not with
disabled
(IE actually gives you an error if you try), and file uploads have unusual behaviour for security reasons - HTMLIFrameElement (though focusing it doesn't do anything useful). Other embedding elements also, maybe, I haven't tested them all.
- Any element with a
tabindex
$ b
...除非 tabindex
是 -1
,这使得焦点变得不可能。
这个行为可能还有其他细微的例外和增加,取决于浏览器。
I'm looking for a definitive list of HTML elements which are allowed to take focus, i.e. which elements will be put into focus when focus()
is called on them?
I'm writing a jQuery extension which works on elements that can be brought into focus. I hope the answer to this question will allow me to be specific about the elements I target.
There isn't a definite list, it's up to the browser. The only standard we have is DOM Level 2 HTML, according to which the only elements that have a focus()
method are HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement and HTMLAnchorElement. This notably omits HTMLButtonElement and HTMLAreaElement.
Today's browsers define focus()
on HTMLElement, but an element won't actually take focus unless it's one of:
…unless the tabindex
is -1
, which makes focus impossible.
There are likely to be other subtle exceptions and additions to this behaviour depending on browser.
这篇关于哪些HTML元素可以获得焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!