使用 JavaScript 或 jQuery 检测哪个表单输入具有焦点 [英] Detect which form input has focus using JavaScript or jQuery

查看:27
本文介绍了使用 JavaScript 或 jQuery 检测哪个表单输入具有焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 JavaScript 或 jQuery 检测哪个表单输入具有焦点?

How do you detect which form input has focus using JavaScript or jQuery?

在函数中,我希望能够确定哪个表单输入具有焦点.我希望能够在直接的 JavaScript 和/或 jQuery 中做到这一点.

From within a function I want to be able to determine which form input has focus. I'd like to be able to do this in straight JavaScript and/or jQuery.

推荐答案

我不确定这是否是最有效的方法,但您可以尝试:

I am not sure if this is the most efficient way, but you could try:

var selectedInput = null;
$(function() {
    $('input, textarea, select').focus(function() {
        selectedInput = this;
    }).blur(function(){
        selectedInput = null;
    });
});

这篇关于使用 JavaScript 或 jQuery 检测哪个表单输入具有焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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