CSS last-child(-1)

前端 未结 2 919
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 20:34

I am looking for a css selector that lets me select the pre-last child of list.

  • 1
  • 2
相关标签:
2条回答
  • 2020-12-22 21:09

    Unless you can get PHP to label that element with a class you are better to use jQuery.

    jQuery(document).ready(function () {
      $count =  jQuery("ul li").size() - 1;
      alert($count);
      jQuery("ul li:nth-child("+$count+")").css("color","red");
    });
    
    0 讨论(0)
  • 2020-12-22 21:17

    You can use :nth-last-child(); in fact, besides :nth-last-of-type() I don't know what else you could use. I'm not sure what you mean by "dynamic", but if you mean whether the style applies to the new second last child when more children are added to the list, yes it will. Interactive fiddle.

    ul li:nth-last-child(2)
    
    0 讨论(0)
提交回复
热议问题