[英]Focus on a non-input/anchor element?
I'm working on some JavaScript that requires a tag that can get focus. 我正在研究一些需要可以获得焦点的标记的JavaScript。 I'd like to be able to use a
<div />
tag, however <div />
tags can't get focus. 我希望能够使用
<div />
标签,但<div />
标签无法获得焦点。
It looks like the only elements that can gain focus are <input />
tags and <a />
tags. 看起来唯一可以获得焦点的元素是
<input />
标签和<a />
标签。 Is there any other way to allow an element to gain focus that isn't an <input />
or <a />
tag? 有没有其他方法可以让元素获得不是
<input />
或<a />
标记的焦点?
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 . 我不能使用
<a />
或<input />
标签,因为我需要能够将内容放在标签内,这样这些标签都不会起作用,除非有办法允许嵌套的<a />
标签,尽管我怀疑它,因为这违背了标准 。 I'm trying to figure out a way to allow a <div />
tag (or any other container element) to get focus. 我试图想出一种方法来允许
<div />
标签(或任何其他容器元素)获得焦点。
What do you mean by "can get focus"? 你能说“关注”是什么意思? Any DOM element can be setup to receive a multitude of javascript events including
click
events. 可以设置任何DOM元素以接收包括
click
事件在内的大量javascript事件。
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. 如果是这样,如果你不能使用
a
标签为您的容器,然后尝试tabindex
属性上的元素。 I'm not sure how cross-browser it is, but at least try it before writing a tabbed ui in javascript yourself. 我不确定它是如何跨浏览器的,但至少在自己用javascript编写标签式ui之前先尝试一下。
Bookmarks are the way to go. 书签是要走的路。
Begin your content with 开始你的内容
<a href="#anchor">
Somewhat of a workaround and not semantically correct, but... 有些解决方法而且在语义上不正确,但......
a#foo { display: block; }
a#foo:hover { cursor: default; }
Then use JavaScript to intercept the onClick event on a#foo
and return false. 然后使用JavaScript拦截在onClick事件
a#foo
并返回false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.