简体   繁体   English

定位元素/ div的下一个实例

[英]Target next instance of an element/div

I have the following HTML structure and JavaScript file: 我有以下HTML结构和JavaScript文件:

.html html的

<li>
 <button class="show-more"></button>
 some more elements
 <div class="hidden"></div>
</li>

JavaScript JavaScript的

$( ".show-more" ).click(function() {
  event.preventDefault();
  $( this ).next().slideToggle( "fast", function() {
  });
});

I need the click event to toggle the next first instance of .hidden , however the click event is targeting the first element after it and not the next instance of .hidden , any ideas how I can go about it? 我需要的单击事件切换下一一审.hidden ,但click事件的目标是之后的第一个元素,而不是下一个实例.hidden我怎么能去了解它,任何想法?

nextAll and first : nextAllfirst

$(this).nextAll('.hidden').first().slideToggle(...);

This question has more about this: Efficient, concise way to find next matching sibling? 这个问题有更多关于这个: 找到下一个匹配兄弟的高效,简洁的方法?

Another possible solution: 另一种可能的方案

$('~.hidden:first', this).slideToggle("fast");

This will match for the first, next sibling of the class .hidden in the context of this . 这将匹配类的第一,下一个兄弟.hidden的背景下this The ~ -selector will reach all following siblings. ~选择器将到达以下所有兄弟姐妹。

Demo 演示

Reference 参考

Next sibling selector 下一个兄弟选择器

first selector 第一选择器

context of selector 选择器的上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM