我对jQuery非常陌生,但是当用户选择(可访问的)一个表行时,我试图显示最近的隐藏表行。
HTML
Click on a row for more info:
<table>
<tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
<tr class="child"><td><p>Blah blah blah.</p>
</td></tr>
<tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
<tr class="child"><td><p>Blah blah blah.</p>
</td></tr>
<tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
<tr class="child"><td><p>Blah blah blah.</p>
</td></tr>
</table>jQuery
$(function() {
$("tr.child").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
$target.closest("tr.child").toggle();
});
});这是一个小提琴。任何帮助都是非常感谢的。
发布于 2015-06-10 16:28:58
.closest()遍历祖先的DOM结构。
对于给定的表结构,应该使用.next()切换下一个tr。
你可以做这样的事
$("table tr").click(function(event) {
$(this).next(".child").toggle();
});发布于 2015-06-10 16:30:18
你所拥有的是好的。首先需要使用<tr>函数找到最接近的closest()元素。
然后使用nextAll()函数查找该元素之后的所有.child类,但只选择第一个返回的
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
$target.closest("tr").nextAll('.child').first().toggle();
});看这把小提琴
https://stackoverflow.com/questions/30762024
复制相似问题