2013-05-15 35 views
0

使用下面的HTML和我需要的目標只有在他們4名或更少的項目旋轉木馬,也就是第二個在這個例子:JQuery的:通過目標的div具有相同的類名有多少孩子/後代有

<div class="book_carousel"> 
     <h2 class="type_one">Carousel 1</h2> 
     <a class="prev disabled">Previous Item</a> 
     <div class="scrollable"> 
      <div class="items"> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
      </div> 
     </div> 
     <a class="next">Next Item</a> 
    </div> 
    <div class="book_carousel"> 
     <h2 class="type_one">Carousel 1</h2> 
     <a class="prev disabled">Previous Item</a> 
     <div class="scrollable"> 
      <div class="items"> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
       <div class="item"> 
        <a href="http://www.place.com" class="thumb"><img src="image.jpg"></a> 
        <p>Info</p> 
        <a class="button" href="#">LOOK</a> 
       </div> 
      </div> 
     </div> 
     <a class="next">Next Item</a> 
    </div> 

一旦我得到了與它四個或更少項目的div保持,這個想法是,我會關掉傳送帶,因爲這不會是neccessary

回答

2

試試這個: -

篩選出.book_carousel,它有4個或更少的div.item並且做某些事情興。過濾器將返回小於或等於4 .items的輪播div。

Demo

$('.book_carousel').filter(function(){ 
     return $(this).find('div.item').length <= 4; 
}).hide(); // Do hide or remove or something with them. 
+0

那太好了,謝謝 – user2383847

相關問題