简体   繁体   English

jQuery:如何检查输入是否集中

[英]Jquery: How to check if input is focused

I am building a mobile web app where the page is set to responsive through screen width and height. 我正在构建一个移动Web应用程序,其中页面设置为通过屏幕的宽度和高度进行响应。 I am facing one issue here. 我在这里面临一个问题。 In a mobile, if I click any input field, the screen size changes due to mobile keypad in some browsers. 在移动设备中,如果单击任何输入字段,则屏幕尺寸会因某些浏览器中的移动键盘而改变。 In this case I don't want the screen to resize. 在这种情况下,我不想调整屏幕大小。 Where as the screen has to resize when we rotate screen. 旋转屏幕时屏幕必须调整大小的位置。 Your help will be greatly appreciated. 对你的帮助表示感谢。 Below is the part of code I am using for that. 以下是我为此使用的代码部分。

function pageresponsive() {
    $('body').css('width', window.screen.width);
    $('body').css('height', window.screen.height);
}

pageresponsive();
var input_click_status = 0;
$("input").click(function() {
    input_click_status = 1;
});

$( "input" ).focus(function() {
    input_click_status = 1;
});

//This might not work as resize event will be set at the time of page load
if(input_click_status == 0) {
    $(window).resize(function() {
        pageresponsive(true);
    });
}

试试这个解决方案

var isFocused =  $("input").is(":focus") 

for version 1.6+ of jquery 适用于jQuery 1.6+版本

$("selector").is(":focus")

:Focus :焦点

you can check by two way: 您可以通过两种方式进行检查:

you can bind an event on input. 您可以在输入上绑定事件。

var is_focused = false;
jQuery('input').bind('focus', function(){
  is_focused = true;
  /* you can write code what ever you want to do on focus.*/
});

or 要么

there is one more function: 还有一个功能:

var is_focused = jQuery("input").is(":focus")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM