使用jquery查找要切换的特定元素

使用jquery查找要切换的特定元素,jquery,find,toggle,Jquery,Find,Toggle,我有这个结构(重复三次) 因为我有三次这样做,所以我的代码必须用不同的类重复代码,因为只有on-sub应该是可见的 我需要这样的东西 onlick THIS_ELEMENT toggle the next SUB class Element AND if a sub class is allready open close it and open this sub class 对于子类中的切换按钮,如下所示: onlick THIS_ELEMENT close this SUB class e

我有这个结构(重复三次)

因为我有三次这样做,所以我的代码必须用不同的类重复代码,因为只有on-sub应该是可见的

我需要这样的东西

onlick THIS_ELEMENT toggle the next SUB class Element AND if a sub class is allready open close it and open this sub class
对于子类中的切换按钮,如下所示:

onlick THIS_ELEMENT close this SUB class element 

您知道如何执行此操作吗?

您要求我们选择(切换)当前元素的下一个同级元素。因此,在该上下文中,只需使用
.next()
获取目标元素

试试看


事件处理程序此引用触发事件的元素。(请注意,只有在使用jQuery或类似框架时才是如此)

因此,在您的示例中,请使用:

$(this).next('.sub').slide(...)

在您的事件处理程序中。

请共享工作正常的JSFIDDLE,但是我与子元素中的触发器有什么关系?它不起作用
onlick THIS_ELEMENT close this SUB class element 
$(document).ready(function(){ 
  $('.main').click(function(){
    $(this).next('.sub').slideToggle(2000);
  }); 
}); 
$(this).next('.sub').slide(...)