css怎么给第4个li~8个li设置样式,有没有比连续写五个li:nth-child好
用jquery的话,就是
用ul>li的:gt(2)和:lt(8)即可处理。
效果如下
但有选择的情况还是建议用css写法
最后nth-child用法如下
:nth-child(2)选取第几个标签,“2可以是你想要的数字”
.demo01 li:nth-child(2){background:#090}
:nth-child(n+4)选取大于等于4标签,“n”表示从整数,下同
.demo01 li:nth-child(n+4){background:#090}
:nth-child(-n+4)选取小于等于4标签
.demo01 li:nth-child(-n+4){background:#090}
:nth-child(2n)选取偶数标签,2n也可以是even
.demo01 li:nth-child(2n){background:#090}
:nth-child(2n-1)选取奇数标签,2n-1可以是odd
.demo01 li:nth-child(2n-1){background:#090}
:nth-child(3n+1)自定义选取标签,3n+1表示“隔二取一”
$('ul li:eq(3)').css('color', 'red');
$('ul li:eq(4)').css('color', 'red');
$('ul li:eq(5)').css('color', 'red');
$('ul li:eq(6)').css('color', 'red');
$('ul li:eq(7)').css('color', 'red');
需要设置样式的标签都定义一个class 然后只对该class设置样式 这样比较好了
css 伪元素选择器
肯定是共用一个类名啊
$('li').filter(function (index) {return index>2&&index<8 }).css('color','#f00')
<!DOCTYPE html>
li{color:red}
li:nth-child(4n){color:blue}
直接在第四个和第八个li之间加类标签 控制就行了
共用一个name,给name添加属性就可以了
相同元素添加相同类名比较好
做法很多,可以用JQ加判断选择,也可以添加公共样式来选择。
ul>li的:gt(4)和:lt(8)