如何使用 jQuery 选择关注它的元素 [英] How to select an element that has focus on it with jQuery

查看:17
本文介绍了如何使用 jQuery 选择关注它的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择具有当前焦点的元素?

How can you select an element that has current focus?

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

There is no :focus filter in jQuery, that is why we can use something like this:

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

推荐答案

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

Really the best way to do it is to setup a handler for the onFocus event, and then set a variable to the ID of the element that has focus.

类似这样的:

var id;

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

这篇关于如何使用 jQuery 选择关注它的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆