如何用jQuery选择一个关注它的元素

你如何选择一个当前焦点的元素?

在jQuery中没有:focus过滤器,这就是为什么我们可以使用这样的东西:

$('input:focus').someFunction();

真的,最好的方法是为onFocus事件设置一个处理程序,然后将一个变量设置为具有焦点的元素的ID。

像这样的东西:

var id;

$(":input").focus(function () {
     id = this.id;
});

$(document.activeElement)将返回当前聚焦的元素,并且比使用伪选择器:focus更快。

来源:http://api.jquery.com/focus-selector/


alert($("*:focus").attr("id"));

我使用jQuery。

它会警告元素的ID正在关注。

我希望它对你有用。

链接地址: http://www.djcxy.com/p/83593.html

上一篇: How to select an element that has focus on it with jQuery

下一篇: jQuery alternative for document.activeElement