使用jquery更改活动选项卡

Change active tab using jquery

本文关键字:选项 活动 jquery 使用      更新时间:2024-03-28

我的页面上有两个选项卡(使用Bootstrap):

<ul class="nav nav-tabs" id="myTab">
  <li class="active"><a href="#home">Home</a></li>
  <li><a href="#profile">Profile</a></li> 
</ul>
<div class="tab-content">
  <div class="tab-pane active" id="home">...</div>
  <div class="tab-pane" id="profile">...</div> 
</div>

当我从配置文件选项卡发送POST请求时,我想使该选项卡处于活动状态,我该如何做到这一点?

看起来您有一个"活动"类来激活选项卡。在这种情况下,你可以做:

$("#home").removeClass("active");  // this deactivates the home tab
$("#profile").addClass("active");  // this activates the profile tab

您的确切示例在文档中,它说要激活选项卡,您需要:

$('#myTab a[href="#profile"]').tab('show')

http://getbootstrap.com/javascript/#tabs

尝试这个

<ul class="nav nav-tabs" id="myTab">
  <li <?php if empty($_POST): ?> class="active" <?php endif; ?> ><a href="#home">Home</a></li>
  <li><a href="#profile" <?php if !empty($_POST): ?> class="active" <?php endif; ?>>Profile</a></li> 
</ul>
<div class="tab-content">
  <div class="tab-pane <?php if empty($_POST): ?> active <?php endif; ?>" id="home">...</div>
  <div class="tab-pane <?php if !empty($_POST): ?> class="active" <?php endif; ?>" id="profile">...</div> 
</div>