JavaScript 焦点控制

4

有没有办法确定网页上当前聚焦的控件?我希望在 ajax 回调之前保存聚焦的控件,并在回调后恢复它。

这是否可以轻松地确定?

谢谢,

AJ

4个回答

3

使用:

document.activeElement

这个功能还没有被官方标准化 (HTML5中将会标准化),但是大多数现代浏览器都支持它。它最初在Internet Explorer中启用,因此所有版本的IE都支持它。Firefox从FF3开始支持它。Chrome也支持它,我认为Safari也支持。


1
如果您正在使用jQuery,您可以使用http://plugins.jquery.com/project/focused插件解决此问题。
// elm is the DOM Element owning the focus or null if no
// DOM Element has the focus
var elm = jQuery.focused();

1

现在许多浏览器都支持document.activeElement

适用于:

  • Firefox
  • IE 6,7,8
  • Chrome
  • Safari
  • Opera

1

尝试使用document.activeElement


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接