开发者

How to get input has focus or not using jquery [duplicate]

开发者 https://www.devze.com 2023-03-20 06:53 出处:网络
This question already has answers here: Closed 11 years ago. How to get input tag of html has focus or not using jquery
This question already has answers here: Closed 11 years ago.

How to get input tag of html has focus or not using jquery

keydown event will work for form if input,image etc. tag has focus. but it will not work it focus is on form but not on any tags like input,image etc. How can I solve this. Please help Thanks in advance

$('#title').focusout(function() { if($(document.activeElement).is(":focus")) { alert('focus'); } else { alert('not focus'); } }); // this will not work开发者_JS百科


you can do it by

if ($("#id").is(":focus")) {
  alert('focus');
}


Its much easier to check by using jquery. Using jquery u can do it like this

if($"#div/inputtocheck").is(":focus"))
{
//do something u want if it is in focus
}

The :focus returns true if the div is in foucs and false if it is not in focus


You can use document.activeElement.

0

精彩评论

暂无评论...
验证码 换一张
取 消