jQuery's “:last-child” selector won't respect <body> as parent on Jsbin?

后端 未结 2 1521
Happy的楠姐
Happy的楠姐 2021-01-19 14:52

I have this simple HTML :


  
    
1
2
3
4&l
2条回答
  •  情深已故
    2021-01-19 15:07

    If you look at the DOM, JS Bin inserts some script elements before the closing tag, which prevents any of the divs from matching div:last-child. Remember that although script elements are (usually) not rendered, they do exist in the DOM just like any other HTML element, and as a result will affect selector matching.

    The last div is in fact the last of its type, even if it isn't the very last child of body; you can verify this by switching to :last-of-type and it will match.

    As mentioned in the comments, Stack Snippets does this as well:

    div:last-child { text-decoration: underline; }
    div:last-of-type { color: red; }
    
      
    Red but no underline

提交回复
热议问题