CSS Selectors - Selecting a specific child element

后端 未结 2 1302
死守一世寂寞
死守一世寂寞 2021-01-14 14:16

Here is my code snippet:

相关标签:
2条回答
  • 2021-01-14 14:38

    If you have the ability to change the output of the shopping cart, you could add a class to the <tr> tag, e.g. <tr class="row01">, <tr class="row02">, etc.

    If you can't change your back-end, then it's a choice of front-end tech. The most cross-browser method is to use jQuery to apply the row classes. The other alternative, CSS3, isn't supported by any current IE; given that this is a shopping cart, you're probably interested in the widest level of browser support.

    Something like:

    $('#shopping-cart-totals-table tr').each(function(n){
        $(this).addClass('row'+n);
    });
    

    Alternatively, if you're only interested in the third item, use jQuery in the same way as CSS3:

    $('#shopping-cart-totals-table tr:nth-child(2) .price').addClass('highlightClass');
    
    0 讨论(0)
  • 2021-01-14 14:42

    With CSS3's :nth-child() it's easy to fulfill the "specific" criterion:

    #shopping-cart-totals-table > tbody > tr:nth-child(2) > td:nth-child(2) .price
    

    Or, an alternative that works more in favor of browser compatibility (does not use CSS3 selectors, assumes exactly two trs and two tds):

    #shopping-cart-totals-table > tbody > tr + tr > td + td .price
    
    0 讨论(0)
提交回复
热议问题