Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

javascript - Focus on a non-input/anchor element?

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.

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What do you mean by "can get focus"? Any DOM element can be setup to receive a multitude of javascript events including click events.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...