2016-01-24 33 views
2

說我有一個名爲項類的多個。只選一個jQuery的單一類

<div class="item"></div> 
<div class="item"></div> 
<div class="item"></div> 

但我只想顯示一個,順序不重要。我怎麼做?

$('.item').show() // this will show all of them 
+1

和一個應該顯示?什麼「序」你在說什麼?恕我直言,目前還不清楚它是什麼要求在這裏 –

回答

4

您可以使用以下方法來從匹配的集合選擇一個特定的元素:

// show the first .item only 
$('.item:first').show(); 
$('.item').first().show(); 

// show the last .item only 
$('.item:last').show(); 
$('.item').last().show(); 

// show the second .item only 
$('.item:eq(1)').show(); 
$('.item').eq(1).show(); 

注意eq需要一個參數是你要定位的元素的索引。

2
$('.item').eq(0).show() // it will display first item