nth-child() or first-child? how to select first AND second child in one

前端 未结 5 969
再見小時候
再見小時候 2020-12-24 13:25

I\'d like to select the first two list items in an unordered list. I can select the first item thus:

ul li:nth-child(1) a {
    background: none repeat scrol         


        
5条回答
  •  无人及你
    2020-12-24 13:50

    Your best bet for cross-browser compatibility would be to use jQuery and assign a class to the list item.

    • http://api.jquery.com/eq-selector/
    • http://api.jquery.com/index/

    Something like...

    $( function() {
    
     $('li').each( function() {
       if( $(this).index() == 1 || $(this).index() == 2 ) {
         $(this).addClass('first_or_second');
       }
     });
    
    });
    

提交回复
热议问题