2

我是一个 javascript 菜鸟,我想知道如何实现这个问题的答案?

Twitter Bootstrap 选项卡:在页面重新加载或超链接时转到特定选项卡

我想在选项卡所在的同一页面上使用此代码....

  <script type="text/javascript">
    // Javascript to enable link to tab
    var url = document.location.toString();
    if (url.match('#')) {
      $('.nav-tabs a[href=#'+url.split('#')[1]+']').tab('show') ;
    } 

    // Change hash for page-reload
    $('.nav-tabs a').on('shown', function (e) {
      window.location.hash = e.target.hash;
    })
  </script>

我应该在包含选项卡的页面的哪个位置插入它。

再次,很抱歉成为这样的菜鸟。

4

2 回答 2

4

引导程序 3解决方案:

<script type="text/javascript">
$(function() {
  // Javascript to enable link to tab
  var url = document.location.toString();
  if (url.match('#')) {
    $('.nav-tabs a[href=#'+url.split('#')[1]+']').tab('show') ;
  }

  // Change hash for page-reload
  $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
    window.location.hash = e.target.hash;
  });
});
</script>
于 2013-10-18T00:53:22.810 回答
1

尝试将代码包装在$(document).ready()函数中

$(document).ready(function() {
 //your code here...
});

您上面的内容将不起作用,因为 DOM 在运行时不会准备好。使用该$(document).ready()函数将延迟执行,直到 dom 加载。它几乎可以在包含选项卡的页面中的任何位置出现。有些人认为它应该放在该head部分内,有些人认为它应该放在最后。但请阅读此处以获得更深入的答案:我应该将 $(document).ready() 放在哪里?

参见:http ://docs.jquery.com/Tutorials:Introducing_ $(document).ready()

于 2013-03-13T00:32:32.900 回答