javascript - Jquery切换具有特定类的最近项目
问题描述
我正在尝试使用特定类切换一些项目。这里的代码问题是什么?
//show hide items with class Network
$(document).ready(function() {
$('.testCategory').click(function() {
$(this).closest('.Network').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="testCategory">Network</a>
<a href="#" class="Network none">RTT</a>
<a href="#" class="Network none">Capacity</a>
<a href="#" class="Network none">Jitter</a>
解决方案
.closest()
遍历 DOM。您可能想要nextAll()
:
$('.testCategory').click(function(){
$(this).nextAll('.Network').toggle();
});
$(document).ready(function() {
$('.testCategory').click(function() {
$(this).nextAll('.Network').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="testCategory">Network</a>
<a href="#" class="Network none">RTT</a>
<a href="#" class="Network none">Capacity</a>
<a href="#" class="Network none">Jitter</a>
推荐阅读
- http-redirect - 如何在打开重定向的情况下在 Postman 测试中检索最终 URL
- python - How can I split a text from a parenthesis in a CSV, and create another column with it
- regex - 将 Unicode 字符与正则表达式匹配
- javascript - 标签为空时如何隐藏?
- mysql - SQL 索引存在,但 drop 命令告诉它不存在
- api - APIs for implementing video playback in embedded Linux (buildroot)
- android - Is there any use of Android:debuggable=true when you don't have any source code?
- c# - 如何从嵌套的 JSON 对象中检索特定属性
- laravel - Laravel - 按特定顺序获取数据……
- python - Python Pandas 中的转置