How to find next similar sibling moving down in the dom tree

后端 未结 1 672
陌清茗
陌清茗 2020-12-12 04:14

What I need over here is while positioned on one of the dt elements I want to jump to the next dt element. How can I achieve this?

         


        
相关标签:
1条回答
  • 2020-12-12 04:58

    What appears to be the obvious option in jquery: .next("dt") is not, because .next() always only returns the very next sibling, applying a filter .next(filter) still only returns the very next sibling, but only if it matches the filter

    jQuery provides two alternatives: .nextUntil() and .nextAll()

    These can be used as:

    nextdt = thisdt.nextUntil("dt").next();
    nextdt = thisdt.nextAll("dt").first();
    

    where nextUntil gets the next siblings until the match (so you then need another next()) and nextAll gets all the matching (so you then need first()).

    In the question's code, this gives the following update:

    jQuery('.accordion > dt').on('click', function() {
        $this = $(this);
        $target = $this.nextAll("dt").first(); 
    

    Example fiddle: https://jsfiddle.net/0wk1mkeq/

    0 讨论(0)
提交回复
热议问题