通过类名称递归查找下一部分

时间:2018-11-13 11:16:43

标签: jquery html

我有一个HTML块,其中仅包含一系列“节”,每个节都包含一个按钮。我想做的是获取单击该按钮的部分,然后再获取下一部分,而我就无法到达它附近的任何地方(!),因此HTML(为了便于阅读而进行了裁剪,因为它包含按钮的div其中包含大量标记)如下:

<section class="showMe">
    <div class="col-md-12 form-group">
         <a class="reg-next-button">Next</a>
    </div>
</section>

<section class="showMe d-none">
    <div class="col-md-12 form-group">
         <a class="reg-next-button">Next</a>
    </div>
</section>

<section class="showMe d-none">
    <div class="col-md-12 form-group">
         <a class="reg-next-button">Next</a>
    </div>
</section>

因此,当单击“按钮”时,将触发以下脚本块:

$('section .reg-next-button').on(click, function (e) {
    var section = $(this).closest("section"); //This gives me the section that the button was clicked from

    //what I've tried to get the 'next' section all of which have been unsuccessful

    var $next = section.next('.showMe');
    var $next = $(this).next('section');
    var $next = $(this).closest('section').find('showMe');
    var $next = $('section.showMe').nextAll();
    var $next = section.next().find('.showMe');

    //once I've got the 'next section I'll do some stuff with adding/removing classes to make the section shown/hidden

});

有人可以帮忙吗?

谢谢, C

1 个答案:

答案 0 :(得分:1)

一旦获得了当前部分,就可以在没有任何输入参数的情况下获得下一部分。在这里您可以找到示例fiddle

$('section .reg-next-button').on('click', function (e) {
var section = $(this).closest("section");
var $next = section.next();    
alert($next.attr('class'));

});