2011-02-03 1 views
0

5 개의 li 요소가 있습니다. 그 중 하나는 세 번째 li이 숨겨져 있다고 말합니다. 네 번째 요소의 색인이 표시되도록 색인을 얻기 전에 필터를 적용하는 방법이 있습니까? 대신 3점포 리에 대한 현재의 호평 된 리의 색인을 얻으십시오 - jquery 호버

<html> 
    <head> 
     <title>test</title> 
     <script type='text/javascript' src='js/jquery-1.4.2.js'></script> 
     <script> 
      $(window).load(function() { 
        jQuery('.addchar').live('hover', function(event) { 
         $("#result").html("Index is:" +$(this).index()); 
        }); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <div id="content"> 
      <div> 
       <ul> 
        <li class="addchar">one </li> 
        <li class="addchar">two </li> 
        <li class="addchar" style="display:none"> three</li> 
        <li class="addchar">four </li> 
        <li class="addchar">five </li> 
       </ul> 
      </div> 
      <div id="result"></div> 
     </div> 
    </body> 
</html> 

답변

2

방법에 대한 사용 visible selector :

$('.addchar:visible').live(... 

편집 :

너무 나쁜, 당신은 다른 방법을 시도 할 수 있다면 그 foreach는 방법 반복으로

$(function() { 
    $('.addchar:visible').each(function(index) { 
     $(this).hover(function() { 
      $("#result").html("Index is: " + index); 
     }); 
    }); 
}); 
+0

나는 당신의 제안을 시도했지만 동일한 결과를 보았습니다. 0과 현재 올라온 요소의 색인 사이에 숨겨진 요소의 총 수를 얻는 것이 가능합니다. 그렇다면 색인을 계산할 수 있습니다. – kiranking

+0

대체 방법을 추가했습니다. – igorw

0

요소 크기 rises.I는 검색과 이용이 link에 working.Thanks 도착했을 때이 느린 된 모든 요소 : 당신을위한 옵션이다.

jQuery('.addchar').live('hover', function (event) { 
var cInd = $(this).index();//current index including hidden element 
var hidLen = $(this).parent().children("li:lt(" + cInd + "):not(:visible)").length;//total hidden before hovered element 
var index = cInd - hidLen; 
$("#result").html("Current index :" + cInd +"<br>Tot Hidden Before Current:" + hidLen+"<br> Actual Index:"+index); 
});