javascript - Linking to specific twitter bootstrap tabs from URL on same page - Stack Overflow

I'm a javascript noob and I'm wondering how do I implement the answer to this question?Twitte

I'm a javascript noob and I'm wondering how do I implement the answer to this question?

Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink

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.

I'm a javascript noob and I'm wondering how do I implement the answer to this question?

Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink

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.

Share Improve this question edited May 23, 2017 at 11:52 CommunityBot 11 silver badge asked Mar 13, 2013 at 0:05 JessiJessi 8211 gold badge14 silver badges27 bronze badges 1
  • If you want to simply show a tab form a link on the same page, you don't need to use the approach do this, instead use the script see this other answer of mine, with a working jsFiddle example. – Marijn Commented Mar 13, 2013 at 7:39
Add a ment  | 

2 Answers 2

Reset to default 4

Bootstrap 3 solution:

<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(function() {
 //your code here...
});

What you have above won't work as the DOM won't be ready when it runs. Using the $(document).ready() function will delay execution until the dom has loaded. 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. But read here for more in-depth answers on that: Where do I put the $(document).ready()?

See: http://docs.jquery./Tutorials:Introducing_$(document).ready()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745211735a4616882.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信