6

单击时选项卡可以很好地工作,但在窗口加载时选择默认选项卡失败。javascript运行,没有错误,但没有任何反应。有任何想法吗?

<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>
4

2 回答 2

10

你的选择器错了,试试这样吧:

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

演示:http: //jsfiddle.net/TjjKE/

于 2012-04-24T19:04:31.927 回答
9

您还可以将默认选项卡设置为活动状态<li class="active"><a href="#whatever">....</a></li>,然后在相应的选项卡上设置<div id="whatever" class="tab-pane active">..</div>并消除对更多 JS 的需要。看看这个

于 2013-03-26T16:17:08.467 回答