简体   繁体   English

从同一页面上的URL链接到特定的Twitter Bootstrap选项卡

[英]Linking to specific twitter bootstrap tabs from URL on same page

I'm a javascript noob and I'm wondering how do I implement the answer to this question? 我是javascript新手,我想知道如何实现此问题的答案?

Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink Twitter Bootstrap选项卡:转到页面重新加载或超链接上的“特定”选项卡

I want to use this code on the same page that the tabs are located.... 我想在标签所位于的同一页面上使用此代码。

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

Where inside the page containing the tabs should I plugg this in. 我应该在包含标签的页面内的哪个位置插入它。

Again, so sorry for being such a noob. 再次,很抱歉成为这样的菜鸟。

Bootstrap 3 solution: Bootstrap 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>

Try wrapping the code in the $(document).ready() function 尝试将代码包装在$(document).ready()函数中

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

What you have above won't work as the DOM won't be ready when it runs. 您上面的内容将无法正常运行,因为DOM在运行时将无法准备就绪。 Using the $(document).ready() function will delay execution until the dom has loaded. 使用$(document).ready()函数将延迟执行,直到dom已加载。 It can go pretty much anywhere in the page containing the tabs. 它几乎可以在包含选项卡的页面中的任何位置。 Some people think it should go within the head section, some think it should go at the end. 有些人认为它应该放在head部分,有些人认为它应该放在结尾部分。 But read here for more in-depth answers on that: Where do I put the $(document).ready()? 但是,请阅读此处以获取有关以下内容的更深入的答案: 我将$(document).ready()放在哪里?

See: http://docs.jquery.com/Tutorials:Introducing_ $(document).ready() 请参阅: http ://docs.jquery.com/Tutorials: Introducing_ $(document).ready()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM