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

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

问题描述



如何检测哪个表单输入具有使用JavaScript或jQuery的焦点?从函数内部我希望能够确定哪个表单输入具有焦点。我希望能够直接使用JavaScript和/或jQuery。

解决方案

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

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


How do you detect which form input has focus using JavaScript or 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天全站免登陆