2015-11-05 95 views
1

我有幾個帶有'resultblock'類的div。我只想顯示前3個div並用jquery隱藏其他人。但下面的代碼不起作用。你能幫我找到我的錯誤嗎?JQuery隱藏了前3個div元素

//pushing all the divs into array 
var results_list = []; 
$('.resultblock').each(function() { 
    results_list.push(this); 
}); 

//hide all the divs 
$('.resultblock').each(function() { 
    $(this).hide(); 
}); 

//show the first 3 divs 
var i; 
for (i = 0; i < 3; ++i) { 
    $(results_list[i]).show(); 
}); 

回答

4

使用:gt選擇

比匹配組內的索引更大的索引選擇的所有元素。

指數:基於零指數

$('.resultblock:gt(2)').hide();