css selector for first list item

后端 未结 2 881
野的像风
野的像风 2021-01-15 05:52

I have the following html structure.

相关标签:
2条回答
  • 2021-01-15 06:49

    Try using first-of-type instead:

    ul > li:first-of-type
    

    This is a CSS3 selector - so is not completely supported across older browsers. See @Boltclock's answer for much better cross-browser support

    0 讨论(0)
  • 2021-01-15 06:51

    It works only if the li is the first child of the ul, i.e. if the script tag is not present.

    Exactly.

    Since you're using a jQuery template, and you're trying to manipulate the first li to give it a class without modifying the HTML source, you may as well do this all with jQuery:

    $('ul > li:first').addClass('first');
    
    0 讨论(0)
提交回复
热议问题