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
490 views
in Technique[技术] by (71.8m points)

javascript - 我如何找出哪个DOM元素具有焦点?(How do I find out which DOM element has the focus?)

I would like to find out, in JavaScript, which element currently has focus.

(我想用JavaScript找出当前关注的元素。)

I've been looking through the DOM and haven't found what I need, yet.

(我一直在浏览DOM,但尚未找到我需要的东西。)

Is there a way to do this, and how?

(有办法做到这一点吗?)

The reason I was looking for this:

(我寻找这个的原因:)

I'm trying to make keys like the arrows and enter navigate through a table of input elements.

(我试图使像箭头键和enter通过输入元素表中导航。)

Tab works now, but enter, and arrows do not by default it seems.

(选项卡现在可以使用,但是可以输入,默认情况下,箭头没有显示。)

I've got the key handling part set up but now I need to figure out how to move the focus over in the event handling functions.

(我已经设置了关键处理部分,但是现在我需要弄清楚如何在事件处理功能中转移焦点。)

  ask by Tony Peterson translate from so

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

1 Reply

0 votes
by (71.8m points)

Use document.activeElement , it is supported in all major browsers.

(使用document.activeElement ,所有主要浏览器都支持它。)

Previously, if you were trying to find out what form field has focus, you could not.

(以前,如果您试图找出哪个表单字段具有焦点,则无法。)

To emulate detection within older browsers, add a "focus" event handler to all fields and record the last-focused field in a variable.

(要在较旧的浏览器中模拟检测,请向所有字段添加“焦点”事件处理程序,并将最后关注的字段记录在变量中。)

Add a "blur" handler to clear the variable upon a blur event for the last-focused field.

(添加一个“模糊”处理程序,以在发生模糊事件时清除最后关注的字段的变量。)

Related links:

(相关链接:)


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

...