隐藏除前4个元素以外的所有元素 [英] Hide all except the first 4 elements

查看:61
本文介绍了隐藏除前4个元素以外的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有无尽的元素.我想做的是隐藏除前4个元素之外的所有元素.(使用:not选择器)

There are endless elements. And I want to do is hide all except the first 4 elements. (with :not selector)

我想使所有单击都可见.. CSS可能吗?

And I want to make a click all visible.. is this possible with css?

<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>/*must hide*/
  <li></li>/*must hide*/
  <li></li>/*must hide*/
  <li></li>/*must hide*/
      ........ /*must hide foreva*/
</ul>

推荐答案

您可以使用

You can use the following solution, using :nth-child:

ul li:nth-child(n+5) {
  display:none;
}
input[type=checkbox]:checked + ul li {
  display:list-item;
}

<input type="checkbox"/>
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li>10</li>
  <li>11</li>
  <li>12</li>
</ul>

您可以使用<输入类型="checkbox"/> 更改项目的可见性.

You can use a <input type="checkbox"/> to change the visibility of the items.

您还可以使用 :nth-​​child :not (计算隐藏项目的另一种方式):

You can also use the :nth-child with :not (other way to calculate the hidden items):

ul li:not(:nth-child(-n + 4)) {
  display:none;
}
input[type=checkbox]:checked + ul li {
  display:list-item;
}

<input type="checkbox"/>
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li>10</li>
  <li>11</li>
  <li>12</li>
</ul>

这篇关于隐藏除前4个元素以外的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆