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

javascript - Can't Set Default in Twitter Bootstrap Toggleable Tab - Stack Overflow

programmeradmin3浏览0评论

The tabs work nicely when clicked but selecting the default tab on window load fails. The javascript runs, no errors, but nothing happens. Any ideas?

<head>
<link href="/assets/bootstrap.css?body=1" media="screen" rel="stylesheet" type="text/css" />
</head>

<script>
  //<![CDATA[
    window.onload=function () {
      $('#client_tab2').tab();
      };
  //]]>
</script>
<ul class='nav nav-tabs' id='tab'>
  <li><a href="#client_tab1" data-toggle="tab">General</a></li>
  <li><a href="#client_tab2" data-toggle="tab">Applications</a></li>
  <li><a href="#client_tab3" data-toggle="tab">Family</a></li>
  <li><a href="#client_tab4" data-toggle="tab">Memos</a></li>
</ul>

<form accept-charset="UTF-8" action="/clients/4512" class="simple_form client tab-content" id="edit_client_4512" method="post" novalidate="novalidate">
  <div class='tab-pane active' id='client_tab1'>
  <legend>General Information</legend>
  </div>

  <div class='tab-pane' id='client_tab2'>
  <legend>Application Information</legend>
  </div>

  <div class='tab-pane' id='client_tab3'>
  <legend>Family</legend>
  </div>

  <div class='tab-pane' id='client_tab4'>
  <legend>Memos</legend>
  </div>
</form>

The tabs work nicely when clicked but selecting the default tab on window load fails. The javascript runs, no errors, but nothing happens. Any ideas?

<head>
<link href="/assets/bootstrap.css?body=1" media="screen" rel="stylesheet" type="text/css" />
</head>

<script>
  //<![CDATA[
    window.onload=function () {
      $('#client_tab2').tab();
      };
  //]]>
</script>
<ul class='nav nav-tabs' id='tab'>
  <li><a href="#client_tab1" data-toggle="tab">General</a></li>
  <li><a href="#client_tab2" data-toggle="tab">Applications</a></li>
  <li><a href="#client_tab3" data-toggle="tab">Family</a></li>
  <li><a href="#client_tab4" data-toggle="tab">Memos</a></li>
</ul>

<form accept-charset="UTF-8" action="/clients/4512" class="simple_form client tab-content" id="edit_client_4512" method="post" novalidate="novalidate">
  <div class='tab-pane active' id='client_tab1'>
  <legend>General Information</legend>
  </div>

  <div class='tab-pane' id='client_tab2'>
  <legend>Application Information</legend>
  </div>

  <div class='tab-pane' id='client_tab3'>
  <legend>Family</legend>
  </div>

  <div class='tab-pane' id='client_tab4'>
  <legend>Memos</legend>
  </div>
</form>
Share Improve this question edited Apr 24, 2012 at 19:07 Andres I Perez 75.4k21 gold badges158 silver badges139 bronze badges asked Apr 24, 2012 at 18:54 Peter KrnjevicPeter Krnjevic 1,11015 silver badges21 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

Your selector is wrong, try it like this instead:

$(window).load(function(){
    $('#tab a[href="#client_tab2"]').tab('show');
});

Demo: http://jsfiddle.net/TjjKE/

You can also set a default tab as active by just <li class="active"><a href="#whatever">....</a></li> and then on the appropriate tab have <div id="whatever" class="tab-pane active">..</div> and eliminate the need for more JS. have a look at this

发布评论

评论列表(0)

  1. 暂无评论