我这下面有一组li,我想给他们加个红色背景,但是就是除了最后一个不加要怎么写?我用下面那样写没用
li:not(li:nth-last-child){ background:red; } <ul> <li></li> <li></li> <li></li> </ul>
光阴似箭催人老,日月如移越少年。
Isn’t that so?
li:not(:last-of-type){ } /**:last-of-type指的是相对父元素的最后一个li元素**/
The answer on the second floor can also be used
li:not(:last-child){ } /**:last-child指的是li元素而且这个li元素是父元素的最后一个元素**/ /**等同于楼主的**/ li:not(:nth-last-child(1)){ }
When the child elements of the ul element are all li elements, the two are the same
<ul> <li> <li> <li><!--第一个和第二个选择器都会忽略这个--> <ul>
But if not all the child elements of the ul element are li and the last element is not li either
<ul> <li> <li> <p> <li> <!--这时候第一个选择器会忽略这个,而第二个选择器会全选上--> <i> <ul>
li:not(:last-child){}
Isn’t that so?
The answer on the second floor can also be used
What’s the difference between the two?
When the child elements of the ul element are all li elements, the two are the same
But if not all the child elements of the ul element are li and the last element is not li either
li:not(:last-child){
}