2013-03-25 1 views
0

두 요소에 포커스가 없는지 어떻게 확인합니까?jquery를 사용하여 여러 요소에 포커스가 없는지 확인하는 방법

텍스트 상자와 드롭 다운 목록이 있습니다. 둘 다 초점에 있지 않으면 드롭 다운을 숨기려고합니다.

다음 코드로 확인할 수 있습니까?

if (!$("#dropdown,#textbox").is(":focus")) 
+1

'시도 ('초점') && ! $ ('# textbox'). is ('focus')) {...}' – qwerty

+2

당신은 단지 코드를 시도해야합니다 : http://jsfiddle.net/ – FishBasketGordo

답변

0

document.activeElement는이 다음 ID가 dropdown 또는 textbox 일치하지 않음을 확인한다 할 일은, 어떤 jQuery를이 필요하지 현재 포커스 요소를 반환합니다!

var focused = document.activeElement.id; 

if (focused != 'dropdown' && focused != 'textbox') { 
    document.getElementById('dropdown').style.display = 'none'; 
} 

또는 jQuery를 버전 :

$('#dropdown').toggle(!$("#dropdown, #textbox").is(":focus")); 
+0

jquery 버전이 저를 위해 작동하지 않았다. –

3
if ($("#dropdown,#textbox").is(":focus")) //its valid it will validate any 
{ 
    $("#dropdown").show(); 
}else{ 
    $("#dropdown").hide(); 
} 
0

보십시오!. (('# 드롭 다운')가 $ 인 경우

if (!$('#dropdown').is(':focus') && !$("#textbox").is(':focus')) { your function here } 
+0

'$ ("# dropdown")'이 객체이기 때문에 항상 true로 평가됩니다 (따라서 true). –

+1

작동하지 않을 경우 두 선택기 모두에 'not'가 필요합니다. ** 편집 : ** 나는 당신의 대답을 편집했습니다. – qwerty

관련 문제