Javascript/jQuery 检测输入是否聚焦

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17614844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:25:22  来源:igfitidea点击:

Javascript/jQuery detect if input is focused

javascriptjquery

提问by southpaw93

How do I detect when .clickevent triggers if textarea is already focused?

.click如果 textarea 已经聚焦,如何检测事件何时触发?

I have this jquery:

我有这个 jquery:

$(".status").on("click","textarea",function(){
        if ($(this) == "focused") {
            // fire this step
        }else{
            $(this).focus();
            // fire this step
    }

回答by Prinzhorn

With pure javascript:

使用纯 javascript:

this === document.activeElement // where 'this' is a dom object

or with jquery's :focuspseudo selector.

或使用 jquery 的:focus伪选择器。

$(this).is(':focus');

回答by sohaiby

If you can use JQuery, then using the JQuery :focus selectorwill do the needful

如果您可以使用 JQuery,那么使用JQuery :focus 选择器将完成所需的工作

$(this).is(':focus');

回答by Vincent Cohen

Did you try:

你试过了吗:

$(this).is(':focus');

Take a look at Using jQuery to test if an input has focusit features some more examples

看看Using jQuery to test if an input has focus它具有更多示例

回答by Praveen

Using jQuery's .is( ":focus" )

使用 jQuery 的.is( ":focus" )

$(".status").on("click","textarea",function(){
        if ($(this).is( ":focus" )) {
            // fire this step
        }else{
                    $(this).focus();
            // fire this step
    }