JQuery隐藏了前3个div元素

时间:2015-11-05 16:18:43

标签: jquery html

我有几个div与' resultblock'类。我想要的只是展示前3个div并用jquery隐藏其他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();
});

1 个答案:

答案 0 :(得分:4)

使用:gt选择器

  

选择匹配集中索引大于索引的所有元素。

     

索引:从零开始的索引

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