开发者

jQuery + CSS element change

I'm using the following code to set all classes of the element to display:none with the exception of the first instance.

How 开发者_Go百科would I modify the code to set the classes all the elements todisplay:none with the exception of the SECOND instance only.

$(document).ready(function () {
    $('selectorForElement').slice(1).css('display', 'none');
});


A conjunction of :not() and :eq() selectors should do it:

$('.selectorForElement:not(:eq(1))').hide();

Live demo.


$('selectorForElement:eq(0), selectorForElement:gt(1)').css('display', 'none');


Try the following

$(document).ready(function () { 
    $('selectorForElement:eq(0)').css('display', 'none'); 
    $('selectorForElement:gt(1)').css('display', 'none'); 
});

The :eq(0) selector will select the first element in the selector (index equals 0). The :gt(1) selector will select all items with an index greater than 1 which skips the first and second elements. The result is everything but the second element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜