[英]How to hide td in a table (efficiently) starting from the second td (included)?
Let's say I have a table like the one below: 假设我有一个像下面这样的表:
<table id='randData'>
<tr>
<td >56</td>
<td >Call me</td>
<td >Blah</td>
...
</tr>
<tr>
</table>
What is the best way to hide the tds starting from the second (included) till the end? 从第二个(包括在内)到最后隐藏tds的最佳方法是什么? Adding a class for the tds that will be hidden is more efficient?
为将要隐藏的tds添加一个类更有效? Is there a way of solving this using an expression with nth-child?
有没有办法使用带有nth-child的表达式来解决这个问题?
I'll present 2 simple ways! 我将介绍两种简单的方法!
The idea: Hide every td
tag, and then un-hide the 1st one. 这个想法:隐藏每个
td
标签,然后取消隐藏第一个标签。
tr > td {
display: none; /* Hidden! */
}
tr > td:first-child {
display: initial; /* Undo hiding on only the 1st td */
}
The idea: Hide every sibling beyond the 1st child 想法:隐藏第一个孩子以外的所有兄弟姐妹
tr > td ~ td {
display: none;
}
The reason I'm giving the 1st option as well as this one is that not everyone likes the ~
selector (which selects all children beyond the child to the left of the ~
symbol in case you're not familiar with it!) 我提供第一个选项以及第一个选项的原因是,并不是每个人都喜欢
~
选择器(如果您不熟悉~
选择器,它将选择~
符号左侧的子代之外的所有子代!)
Unless you are animating with jQuery I prefer to just use classes which I think everyone will agree is the most efficient way. 除非您使用jQuery动画,否则我更喜欢只使用我认为每个人都会同意的类是最有效的方法。 You can select all the
td
elements from the second and up like this in CSS: 您可以在CSS中从第二个开始选择所有
td
元素,如下所示:
tr.hide > td:nth-child(n+2) {
display: none;
}
Now just drop the hide
class on the rows you want to hide the 2nd and up. 现在,将
hide
类放到要隐藏第二个或第二个以上的行上。
$("#randData td").eq(1).nextUntil("tr").hide();
如果您必须使用jQuery,那将是我的直接猜测
Using jquery .not()
and :first
selector. 使用jquery
.not()
和:first
选择器。
$('table tr td:not(:first)').hide();
Example : https://jsfiddle.net/DinoMyte/3dzxsxgq/ 范例: https : //jsfiddle.net/DinoMyte/3dzxsxgq/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.