JQuery隐藏的内容比它应该隐藏的更多

JQuery隐藏的内容比它应该隐藏的更多,jquery,Jquery,我一直在更新我的论坛页面分类列表 为了使用HTML5,我做了大量的结构更改,这使得有必要更改显示/隐藏每个标题类别的JQuery 以下是新的JQuery和HTML5结构: 奇怪的是,这在JSFIDLE上运行得非常好。它完全符合我的预期,但在上,它显示/隐藏所有类别 有人能帮我找出原因并提供解决方案吗?您可能不想隐藏所有同级,而只想隐藏div后面的部分。您不需要使用parent两次: if (show) { $(this).closest("div").next("section").f

我一直在更新我的论坛页面分类列表

为了使用HTML5,我做了大量的结构更改,这使得有必要更改显示/隐藏每个标题类别的JQuery

以下是新的JQuery和HTML5结构:

奇怪的是,这在JSFIDLE上运行得非常好。它完全符合我的预期,但在上,它显示/隐藏所有类别


有人能帮我找出原因并提供解决方案吗?

您可能不想隐藏所有同级,而只想隐藏div后面的部分。您不需要使用parent两次:

if (show) {
    $(this).closest("div").next("section").fadeIn(500); // if possible make the selector it more specific using "div.t"
} else {
    $(this).closest("div").next("section").fadeOut(500);
}

问题是,您的真实页面没有包装单个类别的标签。因此,当您使用兄弟方法时,它会选择列表中的每个标记

$(".sh").click(function() {
    var show = ($(this).text() === "+");

    if (show) {
        $(this).parent().parent("div").siblings("section").fadeIn(500);
    }
    else {
        $(this).parent().parent("div").siblings("section").fadeOut(500);
    }
    $(this).text(show ? "-" : "+");
});
if (show) {
    $(this).closest("div").next("section").fadeIn(500); // if possible make the selector it more specific using "div.t"
} else {
    $(this).closest("div").next("section").fadeOut(500);
}