如何在多个元素上使用.next()

How to use .next() over several elements

本文关键字:next 元素      更新时间:2023-09-26

这是当前代码:

$("input[name='content_use']").click(function() {
    if ($(this).is(":checked")){
        $(this).closest('tr').next().show();
        $(this).closest('tr').next().next().show();
        $(this).closest('tr').next().next().next().show();
    } else{
        $(this).closest('tr').next().hide();
        $(this).closest('tr').next().next().hide();
        $(this).closest('tr').next().next().next().hide();
    }
}); 

:|

编辑:

正如您所看到的,我必须重复使用next()才能到达连续的行。我如何将其编码得更短?


解决方案:nextUntil()感谢@pimvdb

使用.nextAll"获取匹配元素集中每个元素的所有以下同级"

$("input[name='content_use']").click(function() {
    if (this.checked) {
        $(this).closest('tr').nextAll().show();
    } else {
        $(this).closest('tr').nextAll().hide();
    }
});