javascript - 仅根据类切换某些元素
问题描述
我有一个简单的表:
<table>
<tr class="header">
<th>
....
</th>
</tr>
<tr class="level2">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
<tr class="level2">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
</table>
这非常适合将所有内容从一个“标题”切换到下一个。
$('.header').click(function () {
$(this).nextUntil('tr.header').toggle();
});
但我不知道该怎么做是只切换“level2”类的元素并隐藏“level3”。
我已经玩了一段时间了 .toggleClass() 和 .netAll() 但我没有得到它。
解决方案
使用过滤器选择项目
$('.header').click(function () {
var trs = $(this).nextUntil('tr.header')
trs.filter('.level2').toggle();
trs.filter('.level3').hide();
});
.header {
background-color: green;
}
.level2 {
background-color: yellow;
}
.level3 {
background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="header">
<th>
....
</th>
</tr>
<tr class="level2">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
<tr class="level2">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
<tr class="level3">
<th>
....
</th>
</tr>
</table>
推荐阅读
- c++ - 尝试在 __global__ 函数中调用 __device__ 函数时“退出代码 255”
- perl - 是否有一个 Perl 库可以将字体绘制为多边形?
- python - 如何使用python从深度嵌套的json中获取所有键?
- bazel - Bazel 将运行时参数传递给目标
- html - CSS字体导入不返回字体更改
- google-cloud-platform - 谷歌云 sql 只读副本一致性
- c++ - C++ 理解继承中的构造函数
- react-native - 如何在 React Native + Expo 下使用 SAML 对 Firebase 中的用户进行身份验证
- python - 套接字编程尝试发送字符串并将其反转,然后接收反转的字符串
- c - 堆栈增长和mmap堆栈?