css 如何取最后一个指定的元素?

有很多个li,li呈3个一行排列。现在需要控制最后一个li的样式(li:last-child),如果最后一个li是第1~3个,读a样式,如果是第4~6个,读b样式。循环下去 7~9读a,10~12读b... 纯css 如何取?

阅读 5.8k
1 个回答
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        li:last-child:nth-of-type(6n+1),li:last-child:nth-of-type(6n+2),li:last-child:nth-of-type(6n+3){
            color: red;
        }
        li:last-child:nth-of-type(6n+4),li:last-child:nth-of-type(6n+5),li:last-child:nth-of-type(6n){
            color: yellow;
        }
    </style>
</head>
<body>
<ul>

</ul>
<script>
    var ul=document.querySelector('ul');
    var index=1;
    setInterval(function(){
        ul.insertAdjacentHTML('beforeend',`<li>${index++}</li>`)
    },500);
</script>
</body>
</html>

这样试试

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题