gpt4 book ai didi

javascript - 单击元素时,使用另一个类找到相对于该元素最接近的元素并切换它

转载 作者:行者123 更新时间:2023-11-28 19:08:06 26 4
gpt4 key购买 nike

我在这里遗漏了一些东西。当单击带有 one 类的行时,我只想查找带有 two 类的最近行,然后切换(显示/隐藏) 它。

$(".one").on('click', function() {
$(this).find('.two').toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="one">
<td> Hello </td>
</tr>
<tr>
<td> world </td>
</tr>
<tr class="two">
<td> Foo </td>
</tr>
<tr class="two">
<td> Bar </td>
</tr>
</table>

最佳答案

类似这样的事情:

$(".one").on('click', function() {
$(this).nextAll('.two:first').toggle();
});

http://jsfiddle.net/DerekL/5ossufmj/

关于javascript - 单击元素时,使用另一个类找到相对于该元素最接近的元素并切换它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31305558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com