jQuery, 960.gs - Apply 'alpha' and 'omega' classes to every 1st and 4th div
I am creating a layout using the 960 grid system and have div items that are displayed automatically. I need to be able to apply an 'alpha' class to the first and 'omega' class to the fourth e.g. div-alpha,div,div,div-omega,div-alpha,div,div,div-omega.
Using the code below it is app开发者_如何学Golying the alpha class to all of the divs :
var n = $("div.item").length;
$('div .item').filter(function(index) {
return n % 5 == 1;
}).addClass('alpha');
$('div .item').filter(function(index) {
return n % 5 == 5;
}).addClass('omega');
How can I achieve this? Many thanks in advance.
You could do:
var divs = $("div.item");
var length = divs.length
divs.eq(3).addClass('omega');
divs.eq(0).addClass('alfa');
divs.eq(length-1).addClass('omega');
if there is a container that contains all your divs you could also use the nth-child() selector
Try this
var n = $("div.item").length;
$('div .item').filter(function(index) {
return index % 5 == 1;
}).addClass('alpha');
$('div .item').filter(function(index) {
return index % 5 == 5;
}).addClass('omega');
精彩评论