所谓区间选择器,顾名思义就是我不想取指定某一个元素,比如一个div下有10个p元素,我想取中间第3-7个给他们同时加上样式或者干嘛,这时候用css怎么写呢?依照惯例,能用css的我们不用js,下面通过实际demo展示下css3骚操作的强大。
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 100%;
height: 30px;
line-height: 30px;
text-align: center;
font-size: 0;
color: #333;
margin: 100px auto
}
.box p {
border: 1px solid #eee;
display: inline-block;
font-size: 14px;
padding: 0 15px;
margin-left: -1px
}
.box p:nth-of-type(n+3):nth-of-type(-n+7) {
background: #1890ff;
color: #fff;
}
</style>
</head>
<body>
<div class="box">
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
</div>
</body>
</html>
类似功能jq选择器
$( "ul li").slice(start,end) // 参数是下标(起始,结束)
$("ul li:gt(3)") // 列出 index 大于 3 的元素
$("ul li:lt(3)") // 列出 index 小于 3 的元素