2010-05-23 3 views

답변

8

반환되는 요소의 길이 속성을 확인하십시오.

if (!$('#some_ul li:visible').length) { 
    // do something only if all 'li's in a given ul are hidden 
} 

편집 : 숨겨진 변경 볼 수


편집 : 부울 테스트를위한 .length 재산의 사용에 대한 설명. 이 답변 맨 아래의 추가 설명을 참조하십시오.

$('#some_ul li:visible').length은 발견 된 요소의 수를 반환합니다. 숫자 값은 true 또는 false와 동일합니다.

!은 true/false 값이 음수임을 나타냅니다.

따라서 $('#some_ul li:visible').length이 보이는 요소를 찾으면 false을 반환하는 것과 같습니다.

뒤에 !을 배치하면 부울 값이 true으로 바뀝니다. 따라서 visible 요소가 없으면 if()의 코드가 실행됩니다.

그것은 고 정확히 동일하다 : $('#some_ul li:visible').length의 숫자 값을 취하고 == 연산자를 사용하는 부울 값으로 변

if ($('#some_ul li:visible').length == 0) { 
    // do something only if all 'li's in a given ul are hidden 
} 

....


:hidden을 사용하려면 먼저 전체 길이를 가져온 다음 숨겨진 길이와 비교해야합니다.

var total = $('#some_ul li'); 

if ($('#some_ul li:hidden').length == total) { 
    // do something only if all 'li's in a given ul are hidden 
} 

편집 : 귀하의 코멘트에 응답에서, 설명을 위해, 시험 거짓 참으로 동일시하는 몇 가지 값 /가 있습니다. 여기

var someVariable; // someVariable is undefined, equates to false 

var someVariable = ''; // someVariable is an empty string, equates to false 

var someVariable = false; // someVariable is a boolean false, equates to false 

var someVariable = 0; // someVariable is number zero, equates to false 

var someVariable = Number('a'); // someVariable is NaN (Not a Number), equates to false 

var someVariable = null; // someVariable is null, equates to false 

이 참으로 동일시 몇 가지 예입니다 : 여기

false로 동일시 값의 몇 가지 예입니다

var someVariable = "false"; // someVariable is a string, equates to true 

var someVariable = 123; // someVariable is a number greater than 0, equates to true 

var someVariable = -123; // someVariable is a number less than 0, equates to true 

var someVariable = true; // someVariable is a boolean true, equates to true 

var someVariable = !false; // someVariable is a negated value that would 
          // otherwise be false, equates to true 

당신은 어떤의 유효 부울 값에 대해 궁금하다면 값 앞에 !!을 입력하십시오.

alert(!!123); // Alerts true 

첫 번째 !은 유효 부울 값의 반대 값으로 변환합니다. 두 번째 !은 다시 유효한 부울 값으로 변환합니다.같은

var a; // an undefined variable 

alert(a); // alerts undefined, logically equates to 'false' 

alert(!a); // alerts the boolean opposite of undefined, which is 'true' 

alert(!!a); // alerts the converts the boolean opposite 'true' back to 'false' 
+0

li이 숨겨져 있으면 작동합니다. 그는 모든 li이 숨겨 지도록 요청합니다. 그래서'! $ ('# some_ul li : visible'). 길이는 –

+0

을 검사해야하지만 길이를 확인하기 위해서는 실제 요소 수 (숨김 또는 표시)와 길이를 비교해야합니다. 모든 요소가 숨겨져 있습니까? –

+0

@Gaby @Emin - 그래, 내가 그것을 한 번 업데이트했다. 또한': hidden' 셀렉터 버전에 대한 업데이트를 추가했습니다. 메모 주셔서 감사. – user113716

0

당신은 할 수 있습니다 : 예를 들어

나는이를 생각하고

if ($('#some_ul li').is(':hidden')) { 
    // do something only if all 'li's in a given ul are hidden 
} 
0

당신이없이 그 <ul>을 선택

if ($('#some_ul li:hidden').length == $('#some_ul li').length) { 
    // do something only if all 'li's in a given ul are hidden 
} 
1
$("ul:not(:has(li:visible))") 

원하는 것입니다 표시 <li> 아이들. length 속성이 있는지 확인하거나 전혀 없는지 확인하려면 each()으로 전화하십시오.

+0

나는이 솔루션의 단순함을 아주 좋아합니다! 대단히 감사합니다! 저것을 위해 투표하십시오! –