jQuery - 隐藏除第一个之外的所有元素

时间:2012-05-05 13:35:58

标签: jquery loops each

假设我有10个按钮。我想要隐藏除第一个按钮之外的所有按钮。

我试图在jQuery中使用each()来完成它,但它无法正常工作。

这是我的剧本。这只是一个测试,看看我是否可以得到按钮的索引。没有出现错误。

$('button').each(function(index){
    alert(index);
});

其他信息:

我的整个剧本就是这个

$(function(){
   $('div#here').load('test.php'); // This is where all the buttons will come from
   $('button').each(function(index){
       alert(index);
   });
});

3 个答案:

答案 0 :(得分:5)

试试这个:

Slice()提供更好的效果

$('button').slice(1).hide();

答案 1 :(得分:2)

与ThiefMaster相同,但不要忘记您需要等待按钮加载。

您需要使用load的回调:

$(function(){
$('div#here').load('test.php', function(){
   $('button:not(:first)').hide();
}); // This is where all the buttons will come from

});

Doc:http://api.jquery.com/load/

答案 2 :(得分:1)

使用以下其中一项:

$('button:not(:first)').hide();
$('button:gt(0)').hide();