最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - getting the id of a bootstrap tabs with javascript - Stack Overflow

programmeradmin6浏览0评论

I am working on a project and using bootstrap tabs property.

I got this from the bootstrap website:

<div>

  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
    <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
    <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
    <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="home"> AAAAAA</div>
    <div role="tabpanel" class="tab-pane" id="profile">BBBBBB</div>
    <div role="tabpanel" class="tab-pane" id="messages">CCCCC</div>
    <div role="tabpanel" class="tab-pane" id="settings">DDDD</div>
  </div>

</div>

I want to get the id of the tab that is displaying and assing it to

val id

I think i need to use javascript or jquery but i dont have any experience with any of them.

I am working on a project and using bootstrap tabs property. http://getbootstrap./javascript/#tabs

I got this from the bootstrap. website:

<div>

  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
    <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
    <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
    <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="home"> AAAAAA</div>
    <div role="tabpanel" class="tab-pane" id="profile">BBBBBB</div>
    <div role="tabpanel" class="tab-pane" id="messages">CCCCC</div>
    <div role="tabpanel" class="tab-pane" id="settings">DDDD</div>
  </div>

</div>

I want to get the id of the tab that is displaying and assing it to

val id

I think i need to use javascript or jquery but i dont have any experience with any of them.

Share Improve this question asked Jul 27, 2015 at 15:32 AlbertSmAlbertSm 1052 gold badges2 silver badges12 bronze badges 1
  • i am getting my id with in a loop which is similar to id="content-{{$key}}" that's why i am having a hard time finding the id. I tried to add onchange="myFunction{this.id) to the div and look for it in the script as function myFunction(id){val id = id} but it didnt work. – AlbertSm Commented Jul 27, 2015 at 15:42
Add a ment  | 

3 Answers 3

Reset to default 2
   $(".tab-pane").each(function(){
       var id = $(this).attr("id");
       // work with id

       // to modify it
       $(this).attr("id",newval)      

   })

To get the active tab, using JQuery:

var activeTab = $(".tab-content").find(".active");
var id = activeTab.attr('id');

If you want to get the id of your first div you use

var id = document.getElementById("home");

or you can use

var id = document.querySelector("#home");
发布评论

评论列表(0)

  1. 暂无评论