2017-08-07 43 views
-5
<div class="carousel-inner product-slider-inner"> 
<?php while($featurepro = mysqli_fetch_assoc($featurequery)): ?> 
<div class="item"> 
<div class="col-md-2 col-sm-6 col-xs-12 text-center"> 
<a href="#"><img src="<?=$featurepro['image'];?>" class="img-responsive"></a> 
<h5 class="text-center"><?=$featurepro['title'];?></h5> 
<h6 class="text-center"><?=money($featurepro['price']);?></h6> 
<div class="btn btn--border btn--primary btn--animated"><a href="" class="anchor"><span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span></a></div> 
</div> 
</div> 
</div> 

我想用jQuery來添加在第一項活動類,但我不,我如何選擇它在滑蓋更多項特定項目,但只有第一個項目的一類活性HTML CSS的jQuery

+0

加入jQuery代碼 –

+0

我用2個或多個滑塊,它也有項目類,所以我想通過特定的類,例如,可以添加...在CSS中,我們可以聲明類由特定的元素沒有給他的類 –

回答

1

有很多方法使用:first-of-type

如果這不起作用,或者選擇比預期的那麼更多的目標.item例如你需要添加更多的代碼。

$(".product-slider-inner>.item:first-of-type").addClass("active");
.item.active { 
 
    color: red; 
 
    font-size: 120%; 
 
    font-style: italic; 
 
}
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
 

 

 
<div class="carousel-inner product-slider-inner"> 
 
    <?php while($featurepro = mysqli_fetch_assoc($featurequery)): ?> 
 
    <div class="item">Something 
 
    <div class="col-md-2 col-sm-6 col-xs-12 text-center"> 
 
     <a href="#"><img src="<?=$featurepro['image'];?>" class="img-responsive"></a> 
 
     <h5 class="text-center"> 
 
     <?=$featurepro['title'];?> 
 
     </h5> 
 
     <h6 class="text-center"> 
 
     <?=money($featurepro['price']);?> 
 
     </h6> 
 
     <div class="btn btn--border btn--primary btn--animated"><a href="" class="anchor"><span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span></a></div> 
 
    </div> 
 
    </div> 
 
</div>

+0

謝謝你,正是我想要的。 –

+0

嗨你好你能告訴我302重定向問題,我有一個產品頁面發送產品信息到數據庫,但我使用相同的頁面進行驗證。但有時它給我201ok,但有時它會讓我302重定向我的其他地方,但我需要使用header()爲同一頁 –

0

你會想使用.first()方法

$('.item').first().addClass('active') 
+0

對不起,不能工作,你可以告訴其他選項 –

+0

這將選擇第一個元素類'item',如果它不工作,那麼你的代碼有另一個問題。如果你可以發佈更多的信息,我們可以幫助你 – CumminUp07

+0

我在我的網頁上有很多其他的產品類,所以告訴我通過使用他們的父類或其他方法定位特定的產品類 –

0

如果你真的想使用jquery,您可以使用first https://api.jquery.com/first/

若y OU想帶class =「項目」訪問第一個div

$(".item").first().addClass("active");