How to select even or odd elements based on class name

前端 未结 4 1590
既然无缘
既然无缘 2021-01-23 21:05

if you create html layout like so

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-23 21:29

    The problem is that :odd and :even (and their CSS cousins :nth-child(odd) and :nth-child(even)) refer to the order in which the elements appear as children of their parent, not as children with that particular selector.

    This worked for me (Prototype, but it looks like MooTools has similar syntax):

    var odd = $$('.a').filter(function(item, index) {
        return index % 2 == 0;
    });
    var even = $$('.a').filter(function(item, index) {
        return index % 2 == 1;
    });
    

    Edit: it seems you already covered that in the question, boo on me for answering before reading fully.

提交回复
热议问题