如何检查第一个/最后一个元素是否具有特定类?

时间:2016-10-10 13:42:43

标签: jquery

我正在制作标签导航,并遇到麻烦。 如果活动选项卡是第一个,我想禁用上一个按钮,如果最后一个选项卡处于活动状态,则禁用下一个按钮。

如何查看此处的第一个/最后一个标签是否有效?

$(document).ready(function() {

  $('ul.tabs li').click(function() {
    var tab_id = $(this).attr('data-tab');

    $('ul.tabs li').removeClass('current');
    $('.tab-content').removeClass('current');

    $(this).addClass('current');
    $("#" + tab_id).addClass('current');
  })

  $('#previous').click(function() {
    if($('ul.tabs li.current').prev().length != 0 ){
    $('ul.tabs li.current').removeClass('current').prev().addClass('current').click();
}
  })

  $('#next').click(function() {
	if($('ul.tabs li.current').next().length != 0 ){
    $('ul.tabs li.current').removeClass('current').next().addClass('current').click();
		}
  });
})
body {
  margin-top: 100px;
  font-family: 'Trebuchet MS', serif;
  line-height: 1.6
}

.container {
  width: 800px;
  margin: 0 auto;
}

ul.tabs {
  margin: 0px;
  padding: 0px;
  list-style: none;
}

ul.tabs li {
  background: none;
  color: #222;
  display: inline-block;
  padding: 10px 15px;
  cursor: pointer;
}

ul.tabs li.current {
  background: #ededed;
  color: #222;
}

ul.arrownavigation {
  list-style: none;
}

ul.arrownavigation li {
  display: inline-block;
  background: #111;
  color: #fff;
  padding: 10px;
  cursor: pointer;
}

ul.arrownavigation li:active {
  background: #444;
}

ul.arrownavigation li.disabled {
  opacity: 0.7;
}

.tab-content {
  display: none;
  background: #ededed;
  padding: 15px;
}

.tab-content.current {
  display: inherit;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

  <ul class="arrownavigation">
    <li id="previous">Previous</li>
    <li id="next">Next</li>
  </ul>

  <ul class="tabs">
    <li class="tab-link current" data-tab="tab-1">Tab One</li>
    <li class="tab-link" data-tab="tab-2">Tab Two</li>
    <li class="tab-link" data-tab="tab-3">Tab Three</li>
    <li class="tab-link" data-tab="tab-4">Tab Four</li>
  </ul>

  <div id="tab-1" class="tab-content current">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
  <div id="tab-2" class="tab-content">
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
  <div id="tab-3" class="tab-content">
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  </div>
  <div id="tab-4" class="tab-content">
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>

</div>
<!-- container -->

2 个答案:

答案 0 :(得分:1)

您可以像这样更改点击功能

$('ul.tabs li').click(function() {
    var tab_id = $(this).attr('data-tab');
    var index = $(".tabs li").index(this);

    if (index == 0) 
    {
      $('#previous').hide();
    } 
    else if (index + 1 == $(".tabs li").length) 
    {
      $('#next').hide();
    } 
    else 
    {
      $('#next').show();
      $('#previous').show();
    }

    $('ul.tabs li').removeClass('current');
    $('.tab-content').removeClass('current');

    $(this).addClass('current');
    $("#" + tab_id).addClass('current');
})

答案 1 :(得分:1)

您需要做的是检查第一个和最后一个标签是否包含current课程。下面稍微修改过的脚本(更通用,可重复使用):

&#13;
&#13;
$('.tabs-container').each(function(i) {

    var tabsContainer = $(this);
    var tabsNavigation = tabsContainer.find('.arrownavigation');
    var tabsLinks = tabsContainer.find('.tab-link');
    var tabLinkFirst = tabsContainer.find('.tab-link:first-child');
    var tabLinkLast = tabsContainer.find('.tab-link:last-child');
    var tabsContent = tabsContainer.find('.tab-content');
    var tabsPrevious = tabsContainer.find('.tab-previous');
    var tabsNext = tabsContainer.find('.tab-next');
    
    /* This is the function you need: */
    function checkArrows() {
    	tabsPrevious.toggleClass('tab-disabled', tabLinkFirst.hasClass('current'));
        tabsNext.toggleClass('tab-disabled', tabLinkLast.hasClass('current'));
    };
    checkArrows();

    tabsLinks.click(function() {
        var tabSelector = $(this).data('tab');
        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');
        $(this).addClass('current');
        $(tabSelector).addClass('current');
        checkArrows();
    });

    tabsPrevious.click(function() {
        var currentTab = tabsContainer.find('.tabs li.current');
        if (currentTab.prev().length > 0) {
            currentTab.removeClass('current').prev().addClass('current').trigger('click');
        }
    });

    tabsNext.click(function() {
        var currentTab = tabsContainer.find('.tabs li.current');
        if (currentTab.next().length > 0) {
            currentTab.removeClass('current').next().addClass('current').trigger('click');
        }
    });

})
&#13;
body {
    margin-top: 100px;
    font-family: 'Trebuchet MS', serif;
    line-height: 1.6
}

.tabs-container {
    width: 800px;
    margin: 0 auto;
}

ul.tabs {
    margin: 0px;
    padding: 0px;
    list-style: none;
}

ul.tabs li {
    background: none;
    color: #222;
    display: inline-block;
    padding: 10px 15px;
    cursor: pointer;
}

ul.tabs li.current {
    background: #ededed;
    color: #222;
}

ul.arrownavigation {
    list-style: none;
}

ul.arrownavigation li {
    display: inline-block;
    background: #111;
    color: #fff;
    padding: 10px;
    cursor: pointer;
}

ul.arrownavigation li:active {
    background: #444;
}

ul.arrownavigation li.disabled {
    opacity: 0.7;
}

.tab-content {
    display: none;
    background: #ededed;
    padding: 15px;
}

.tab-content.current {
    display: inherit;
}

ul.arrownavigation li.tab-disabled {
    background: none #ccc;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tabs-container">

    <ul class="arrownavigation">
        <li class="tab-arrow tab-previous">Previous</li>
        <li class="tab-arrow tab-next">Next</li>
    </ul>

    <ul class="tabs">
        <li class="tab-link current" data-tab=".tab-1">Tab One</li>
        <li class="tab-link" data-tab=".tab-2">Tab Two</li>
        <li class="tab-link" data-tab=".tab-3">Tab Three</li>
        <li class="tab-link" data-tab=".tab-4">Tab Four</li>
    </ul>

    <div class="tab-content tab-1 current">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </div>
    <div class="tab-content tab-2">
        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
    <div class="tab-content tab-3">
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    </div>
    <div class="tab-content tab-4">
        Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </div>

</div>
&#13;
&#13;
&#13;

同样在JSFiddle