使用最近距离的jQuery遍历

使用最近距离的jQuery遍历,jquery,jquery-selectors,jquery-traversing,Jquery,Jquery Selectors,Jquery Traversing,HTML: 我有两个函数来显示/隐藏tr。我现在的代码可以工作,但它也会关闭其他元素。做这件事的优雅方式是什么?我相信在1.3版中添加了最接近的选择器,所以就使用它吧 最接近的选择器我相信是在1.3版中添加的,所以只需使用它即可 这应该可以做到: function ShowBox() { var that = this; $(function () { $(".commentBox").show(); //$(tha

HTML:


我有两个函数来显示/隐藏tr。我现在的代码可以工作,但它也会关闭其他元素。做这件事的优雅方式是什么?

我相信在1.3版中添加了最接近的选择器,所以就使用它吧

最接近的选择器我相信是在1.3版中添加的,所以只需使用它即可

这应该可以做到:

function ShowBox() {
        var that = this;
        $(function () {
            $(".commentBox").show();
            //$(that).closest('tr').siblings().show();
        });
    }
    function HideBox() {
        var that = this;
        $(function () {
            $(that).siblings(".foo").attr("checked", false);
            $(that).siblings(".textComment").empty();
            $(".commentBox").hide();
        });
    }  

顺便说一句,对于“优雅”,请从HTML中删除
onclick
属性

然后使用以下功能添加单击功能:

function ShowBox() {
    $(this).closest ('tr').next ('tr.commentBox').show ();
}

function HideBox() {
    var jThis   = $(this);

    jThis.siblings(".foo").attr("checked", false);
    jThis.siblings(".textComment").empty ();
    jThis.closest ('tr.commentBox').hide ();
} 
$(document.ready()

中,应该这样做:

function ShowBox() {
        var that = this;
        $(function () {
            $(".commentBox").show();
            //$(that).closest('tr').siblings().show();
        });
    }
    function HideBox() {
        var that = this;
        $(function () {
            $(that).siblings(".foo").attr("checked", false);
            $(that).siblings(".textComment").empty();
            $(".commentBox").hide();
        });
    }  

顺便说一句,对于“优雅”,请从HTML中删除
onclick
属性

然后使用以下功能添加单击功能:

function ShowBox() {
    $(this).closest ('tr').next ('tr.commentBox').show ();
}

function HideBox() {
    var jThis   = $(this);

    jThis.siblings(".foo").attr("checked", false);
    jThis.siblings(".textComment").empty ();
    jThis.closest ('tr.commentBox').hide ();
} 

$(document.ready()

中,为什么不使用
最近的
?将函数传递给
文档。函数中的ready
是不必要的。那么为什么不使用
最近的
?将函数传递给
文档。函数内部的ready
是不必要的。