Calling twitter bootstrap tab function from within another function

时间:2023-01-02 16:45:55

With bootstrap-tab.js I want to activate a tab, calling it from within another function. Html:

使用bootstrap-tab.js我想激活一个选项卡,从另一个函数中调用它。 HTML:

<a onClick='activate_tab()'>Activate Tab</a>
<ul class='nav nav-tabs hydro' id='nav_tabs'>
  <li class='active'><a href='#1' data-toggle='tab'>First</a></li>
  <li class=''><a href='#2' data-toggle='tab'>Second</a></li>
  <li class=''><a href='#3' data-toggle='tab'>Third</a></li>
</ul>
<div class='tabbable'>
  <div class='tab-content'>
    <div class='tab-pane active' id='1'>content first</div>
    <div class='tab-pane ' id='2'>content second</div>
    <div class='tab-pane ' id='3'>content third</div>
  </div>
</div>

I am able to activate the tab on page load with the following:

我可以使用以下内容激活页面加载时的选项卡:

<script>jQuery(function($){$('#nav_tabs li:eq(2) a').tab('show');})</script>

And the tabs activate properly by clicking on the respective anchor elements. The following does not work:

通过单击相应的锚元素,可以正确激活选项卡。以下不起作用:

function activate_tab(){
    //process other data...then goto tab
    alert(1);
    $('#nav_tabs li:eq(2) a').tab('show');
    alert(2);
}

I am new to javascript/jQuery.

我是javascript / jQuery的新手。

[EDIT] I stripped the original page down to just what's shown above and it works. So, likely a conflict with other functions. Sorry to waste your time.

[编辑]我将原始页面剥离到上面显示的内容并且有效。所以,可能与其他功能发生冲突。抱歉浪费你的时间。

1 个解决方案

#1


1  

try this if you dont

试试这个,如果你不这样做

    <script>
    jQuery(function($){

      function activate_tab(){
        //process other data...then goto tab
        alert(1);
        $('#nav_tabs li:eq(2) a').tab('show');
        alert(2);
      }

    });
   </script>

#1


1  

try this if you dont

试试这个,如果你不这样做

    <script>
    jQuery(function($){

      function activate_tab(){
        //process other data...then goto tab
        alert(1);
        $('#nav_tabs li:eq(2) a').tab('show');
        alert(2);
      }

    });
   </script>