我如何select除最后一个孩子之外的所有孩子?

我将如何select所有,但最后一个孩子使用CSS3select器?

例如,只得到最后一个孩子将是div:nth-last-child(1)

你可以使用negation伪类:not()对付:last-child伪类 。 引入了CSSselect器3级,它在IE8或以下版本中不起作用:

 :not(:last-child) { /* styles */ } 

简单一点:

你可以将你的风格应用到所有的div,并重新初始化最后一个:last-child

例如在CSS中

 .yourclass{ border: 1px solid blue; } .yourclass:last-child{ border: 0; } 

或在SCSS中

 .yourclass{ border: 1px solid rgba(255, 255, 255, 1); &:last-child{ border: 0; } } 
  • 容易阅读/记忆
  • 快速执行
  • 浏览器兼容 (IE9 +,因为它仍然是CSS3)

当IE9来临时,它会更容易。 很多时候,你可以把问题切换到一个要求:第一个孩子和风格的元素的对面(IE7 +)。

尼克Craver的解决scheme的作品,但你也可以使用这个:

 :nth-last-child(n+2) { /* Your code here */ } 

CSS技巧的Chris Coyier为此做了一个很好的testing 。

在selectivizr中使用nick craver的解决scheme可以实现跨浏览器解决scheme(IE6 +)

从最后find元素,使用

 <style> ul li:nth-last-of-type(3n){ color:#a94442} /**/ </style>