简体   繁体   中英

JQuery, Bootstrap navs - how do I add prev/next buttons?

I am using bootstrap navs for my page and I want to add next and prev buttons to my tab content.

HTML

<ul class="nav nav-tabs nav-justified">
  <li class="active"><a href="#overview">Overview</a></li>
  <li><a href="#contact">Contact</a></li>
  <li><a href="#randomtab">Randomtab</a></li>
</ul>
<div class="tab-content">
  <div id="overview" class="tab-pane fade in active">

    <a href="#contact" class="btn btn-info pull-right">Next</a>

  </div>
  <div id="contact" class="tab-pane fade in active">
  </div>
  <div id="randomtab" class="tab-pane fade in active">
  </div>
</div>

Javascript

$(document).ready(function() {
  $(".nav-tabs a").click(function() {
    $(this).tab('show');
  });
  $('.nav-tabs a').on('shown.bs.tab', function(event) {
    var x = $(event.target).text(); // active tab
    var y = $(event.relatedTarget).text(); // previous tab
    $(".act span").text(x);
    $(".prev span").text(y);
  });
});

I tried to add this JavaScript code but I don't know how to write a[href=(this).href]

$(".tab-pane a").click(function() {
  $('a[href="(this.href)"]').tab('show'); //this row is wrong
});
$('.tab-pane a').on('shown.bs.tab', function(event) {
  var x = $(event.target).text(); // active tab
  var y = $(event.relatedTarget).text(); // previous tab
  $(".act span").text(x);
  $(".prev span").text(y);
});

This is my solution, maybe there is a better one but I don't know...Thank you.

PS: Please leave comment below if you don't understand my question, so I can edit this post.

EDIT

This is the working solution for bootstrap navs:

thanks to Arun P Johny

<ul class="nav nav-tabs nav-justified">
  <li class="active"><a href="#overview">Overview</a></li>
  <li><a href="#contact">Contact</a></li>
  <li><a href="#randomtab">Randomtab</a></li>
</ul>
<div class="tab-content">
  <div id="overview" class="tab-pane fade in active">

    <a href="#contact" class="btn btn-info pull-right">Next</a>

  </div>
  <div id="contact" class="tab-pane fade in active">

    <a href="#overview" class="btn btn-info pull-left">Prev</a>

    <a href="#randomtab" class="btn btn-info pull-right">Next</a>

  </div>
  <div id="randomtab" class="tab-pane fade in active">

    <a href="#contact" class="btn btn-info pull-left">Prev</a>

  </div>
</div>
$(document).ready(function() {
  $(".nav-tabs a").click(function() {
    $(this).tab('show');
  });
  $('.nav-tabs a').on('shown.bs.tab', function(event) {
    var x = $(event.target).text(); // active tab
    var y = $(event.relatedTarget).text(); // previous tab
    $(".act span").text(x);
    $(".prev span").text(y);
  });
  $(".tab-pane a").click(function() {
    $('a[href="' + $(this).attr('href') + '"]').tab('show');
  });
  $('.nav-tabs a').on('shown.bs.tab', function(event) {
    var x = $(event.target).text(); // active tab
    var y = $(event.relatedTarget).text(); // previous tab
    $(".act span").text(x);
    $(".prev span").text(y);
  });
});

我认为问题在于href属性将具有绝对路径,因此请尝试

$('a[href="'+ $(this).attr('href')+'"]').tab('show');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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