Javascript 使用jQuery显示离选定对象最近的隐藏行()

Javascript 使用jQuery显示离选定对象最近的隐藏行(),javascript,jquery,Javascript,Jquery,我对jQuery非常陌生,但是当用户选择它上面的(可视的)行时,我尝试显示最近的隐藏表行 HTML 这是一本书。感谢您的帮助 .closest()遍历祖先上的DOM结构 对于给定的表结构,应使用 你可以这样做 $("table tr").click(function(event) { $(this).next(".child").toggle(); }); 这是一个演示您所拥有的一切都很好。您需要首先使用closest()函数查找最近的元素 然后使用nextAll() $("table

我对jQuery非常陌生,但是当用户选择它上面的(可视的)行时,我尝试显示最近的隐藏表行

HTML

这是一本书。感谢您的帮助

.closest()
遍历祖先上的DOM结构

对于给定的表结构,应使用

你可以这样做

$("table tr").click(function(event) {
    $(this).next(".child").toggle();
});

这是一个演示

您所拥有的一切都很好。您需要首先使用
closest()
函数查找最近的
元素

然后使用
nextAll()

$("table").click(function(event) {
    event.stopPropagation();

    var $target = $(event.target);
    $target.closest("tr").nextAll('.child').first().toggle();
});
看到这把小提琴了吗


最近()用于查找祖先,而不是孩子。在演示中使用find(),如果单击一个可见的
tr.child
,它也会切换下一个
tr.child
s。当用户在其上方选择(可见的一个)时提到的OP
$("table tr").click(function(event) {
    $(this).next(".child").toggle();
});
$("table").click(function(event) {
    event.stopPropagation();

    var $target = $(event.target);
    $target.closest("tr").nextAll('.child').first().toggle();
});