简体   繁体   English

jQuery - 如何使用parents()来访问1级和2级?

[英]jQuery - How to use parents() to access both 1 and 2 levels up?

I have HTML that looks like this: 我有这样的HTML:

<fieldset class="vertical-tabs-pane">
   <div class="vertical-tab-navigation">
       <a class="previous-tab" title="Go back to previous step" href="#">Back</a>
       <a class="next-tab" title="Continue to next step" href="#">Next</a>
   </div>
</fieldset>

<fieldset class="vertical-tabs-pane">
   <div class="vertical-tab-navigation">
       <a class="previous-tab" title="Go back to previous step" href="#">Back</a>
       <a class="next-tab" title="Continue to next step" href="#">Next</a>
   </div>
</fieldset>

<fieldset class="vertical-tabs-pane">
   <div>
      <fieldset id="multistep-group_attendees" class="group-attendees">
          <div class="vertical-tab-navigation">
            <a class="previous-tab" title="Go back to previous step" href="#">Back</a>
            <a class="next-tab" title="Continue to next step" href="#">Next</a>
          </div>
      </fieldset>
   </div>   
</fieldset> 

So: 3 vertical-tabs-pane fieldsets, each with a navigation div in the bottom. 所以:3个vertical-tabs-pane字段集,每个字段集的底部都有一个导航div。 But the 3rd set of nav is nested two levels deeper than the rest. 但是第三组导航比其他导航嵌套了两层。

jQuery like so: 像这样的jQuery:

$(".next-tab").click(function() {
    $(this).parents('fieldset.vertical-tabs-pane').hide().next('fieldset.vertical-tabs-pane').show();
    return false;
});

$(".previous-tab").click(function() {
    $(this).parents('fieldset.vertical-tabs-pane').hide().prev('fieldset.vertical-tabs-pane').show();
    return false;
});

This works perfectly for the first two fieldsets; 这适用于前两个场集; clicking 'back' and 'next' swaps you over to the next fieldset. 点击“后退”和“下一步”将您切换到下一个字段集。 But it doesn't work for the 3rd set, even though it seems to me it should still traverse up the tree to the appropriate parental fieldset. 但它不适用于第3组,即使在我看来它仍然应该遍历树到适当的父级字段集。

What do I need to change? 我需要改变什么? I am fine with having special functions $("#multistep-group-attendees .next-tab") for the 3rd fieldset if that makes it easier. 如果使第三个字段集更容易,我可以使用特殊函数$("#multistep-group-attendees .next-tab")

Thanks for any assistance! 谢谢你的帮助!

The problem may not be with the parents method, but with the next and prev ones. 该问题可能不是与parents方法,但随着nextprev人。 These methods do not cycle (try the demo ). 这些方法不循环(尝试演示 )。 If there's no previous element, prev won't select the last one, and next works the same way. 如果没有先前的元素,则prev不会选择最后一个元素, next工作方式相同。

The following code should do it 以下代码应该这样做

$(".next-tab").click(function() {
    var current = $(this).closest('fieldset.vertical-tabs-pane');
    var next = current.next('fieldset.vertical-tabs-pane');
    if (! next.length ) next = current.siblings(':first');
    current.hide();
    next.show();
    return false;
});

$(".previous-tab").click(function() {
    var current = $(this).closest('fieldset.vertical-tabs-pane');
    var prev= current.prev('fieldset.vertical-tabs-pane');
    if (! prev.length ) prev = current.siblings(':last');
    current.hide();
    prev.show();
    return false;
});

Using closest() and checking for next/previous existence seems to do the trick.. 使用closest()并检查next / previous存在似乎可以解决问题。

demo http://jsfiddle.net/gaby/VbgDg/ 演示 http://jsfiddle.net/gaby/VbgDg/

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

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