2015-11-05 98 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();