检查是否有任何输入元素在焦点上 [英] Check to see if any Input elements are in focus

查看:91
本文介绍了检查是否有任何输入元素在焦点上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jquery脚本,当按下键盘上的任意键时会显示一个div.我想向脚本添加一个条件,该条件仅在页面上没有其他输入区域(textarea或texfields)时才运行该脚本.这样,您实际上可以在页面的其余部分键入内容而不显示div.

I have a jquery script that displays a div when any key on the keyboard is pressed. I'd like to add a condition to the script that will only run the script if no other input area (textarea or texfields) are in focus on the page. That way you can actually type on the rest of the page without showing the div.

$(document).on('keydown', function (e) {
    if (!$('#test').is(':visible')) {
        //######## IF (every input is not active....) {
        if (65 <= e.keyCode && e.keyCode <= 90) {
            $(elem).fadeIn();
            $('#textarea').val('');
            $('#textarea').focus();
            $('#textarea').val(temp);
        }
    }
});

谢谢.如有必要,我可以为页面上的所有其他textarea赋予相同的ID.

Thanks. I can give every other textarea on the page the same ID if that is necessary.

推荐答案

使用jQuery的 focus 伪选择器

Use jQuery's focus pseudoselector

if ( $('input:focus').length > 0 ) {  return; }

或者为您的代码示例

if ( $('input:focus').length == 0) { ... }

这篇关于检查是否有任何输入元素在焦点上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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