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
Javascript/jQuery detect if input is focused
提问by southpaw93
How do I detect when .click
event 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 :focus
pseudo 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
回答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
}