专注于非输入/锚点元素? [英] Focus on a non-input/anchor element?
问题描述
我正在研究一些需要可以获得焦点的标签的JavaScript。我想要使用< div />
标签,但是< div />
标签无法获得关注。
I'm working on some JavaScript that requires a tag that can get focus. I'd like to be able to use a <div />
tag, however <div />
tags can't get focus.
看起来像唯一可以获得关注的元素是< input />
标签和< a />
标签。有没有其他方法允许元素获得不是< input />
或< a />
标签?
It looks like the only elements that can gain focus are <input />
tags and <a />
tags. Is there any other way to allow an element to gain focus that isn't an <input />
or <a />
tag?
我不能使用< a />
或< input />
标签,因为我需要能够将内容放在标签内,所以这些标签都不会工作,除非有办法允许嵌套的< a />
标签,虽然我怀疑,因为违反标准。我试图找出一种允许< div />
标签(或任何其他容器元素)获取焦点的方法。
I can't use <a />
or <input />
tags because I need to be able to place content inside of the tag so neither of those tags will work, unless there's a way to allow nested <a />
tags, although I doubt it since that goes against the standard. I'm trying to figure out a way to allow a <div />
tag (or any other container element) to get focus.
推荐答案
可以得到焦点是什么意思?任何DOM元素都可以设置为接收大量的JavaScript事件,包括点击
事件。
What do you mean by "can get focus"? Any DOM element can be setup to receive a multitude of javascript events including click
events.
或者你是说可以用键盘标签?如果是,如果您不能使用 a
标签作为您的容器,请尝试 tabindex
属性。我不知道如何跨浏览器,但至少在您自己编写一个标签为ui之前尝试过。
Or do you mean "can be tabbed to with the keyboard"? If so, and if you cannot use an a
tag as your container, then try the tabindex
property on your elements. I'm not sure how cross-browser it is, but at least try it before writing a tabbed ui in javascript yourself.
这篇关于专注于非输入/锚点元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!