简体   繁体   English

选项卡bootstarp处于活动状态时如何更改标题文本?

[英]How can change text of title when tab bootstarp was been active?

I have a tab bootstarp and a title for my page and I want to change the title of page with click on tab is active but my jquery code donot work correctly. 我有一个选项卡bootstarp和一个页面标题,我想通过单击选项卡来更改页面标题,但我的jquery代码无法正常工作。

 $(document.body).on('click', '.chooseChangePass li a', function(e) { if ($("#chPass").hasClass("active")) { $("#TitlePage").html("change Password"); } else { $("#TitlePage").html("change UserName"); } if ($("#chUserName").hasClass("active")) { $("#TitlePage").html("change UserName"); } else { $("#TitlePage").html("change Password "); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 id="TitlePage">change password</h1> <ul class="chooseChangePass"> <li class="active" id="chPass"><a href="#changePassword" data-toggle="tab">change password</a></li> <li id="chUserName"><a href="#changeUserName" data-toggle="tab">change UserName</a></li> </ul> <div class="tab-content"> <div role="tabpanel" class="tab-pane fade" id="changeUserName"> changeUserName </div> <div role="tabpanel" class="tab-pane fade in active" id="changePassword"> changePassword </div> </div> 

You are missing code, that toggles active class. 您缺少代码,无法切换active类。 Add bootstrap.min.js file. 添加bootstrap.min.js文件。 Also, else statements in your code are redundant. 另外,代码中的else语句是多余的。

Correct way of changing page title after tab changes is to use Bootstrap navs events . 更改选项卡后更改页​​面标题的正确方法是使用Bootstrap导航事件 You need to listen for shown.bs.tab event. 您需要监听shown.bs.tab事件。

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  $('#page-title').html(e.target.text);
});

Here's working example with Bootstrap 4.1.3: https://codepen.io/anon/pen/OrJOQE?editors=1010 这是Bootstrap 4.1.3的工作示例: https ://codepen.io/anon/pen/OrJOQE?editors =1010

Be sure to lookup documentation for Boostrap version that you are using, as there were significant changes between version 3 and 4. And change tab selector to named one, if you are planning to have more tab groups on same page. 请确保查找正在使用的Boostrap版本的文档,因为版本3和版本4之间有重大变化。如果计划在同一页面上使用更多选项卡组,请将选项卡选择器更改为命名的选择器。

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

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