2013-09-30 4 views
0

javaScript jQuery에는 display : none이있는 특정 클래스의 요소를 측정하는 방법이 있습니까? 예를 들어속성을 사용하여 클래스의 길이를 측정하는 방법 display : none

:

<div class="x" style="display: none;"></div> 
<div class="x" style="display: none;"></div> 
<div class="x"></div> 

클래스 = "X"3 개 요소가있는 한 내가 경고 "3"을 얻을 것이다 아래에이 코드를 수행합니다.

var n = document.getElementsByClassName('x').length; 
alert(n); 

내 경보가 display : none 인 2 개의 클래스 "x"만 표시하도록 올바른 선택기는 무엇입니까?

도움 주셔서 감사합니다.

답변

3

클래스 이름과 :hidden selector을 섞어보십시오. 바닐라 JS에서

다음
var list = $('.x:hidden'); //select all elements with class x and are hidden. 

Demo

:

var n = document.getElementsByClassName('x'); //get the elements with class 
var nodeList = []; 
for(var i=0, len = n.length; i<len; i++){ //loop through them 
    if(n[i].style.display === "none") nodeList.push(n[i]); //check for display property value and push the element to the list. 
} 
alert(nodeList.length); //You have the list of elements with desired properties. 

Fiddle

+0

어떤 유사한 셀렉터 사용하여 자바 스크립트 것입니까? – enriqg9

+0

@ enriqg9가 답변을 업데이트했습니다. – PSL

+0

@ enriqg9': hidden'은 css 스펙이 아니기 때문에 선택자가 없습니다. css 스펙이 있다면 비슷한 선택기를'document.querySelectorAll'에서 사용할 수 있습니다. – PSL

관련 문제