我正在尝试构建一个非常简单的通用切换功能,其中切换“开关”具有类
示例HTML:
问题在于如果
那么,如何仅选择下一个
.toggle
,然后我希望它toggle()
下一个具有类.toggle-content
的元素。示例HTML:
<p>
<a href="#" class="toggle">Toggle the thing</a>
</p>
<p class="toggle-content hidden">I'm totally hidden right now</p>
现在我会使用以下方法来切换:
$(".toggle").click(function() {
$(this).parent().next('.toggle-content').toggle();
});
问题在于如果
.toggle
类在DOM中的深度更深,我必须根据它的深度/浅度不断添加更多的parent()
,这样就会很麻烦。那么,如何仅选择下一个
.toggle-content
实例而不必使用大量的parent()
和next()
呢?
<p>
标签实际上可以是任何标签(fieldset、legend、div、section等),所以最好不要设置它。 - Shpigford$(this).closest('.toggler').next(...
- user113716