【问题标题】:Bootstrap tabs opening tabs on another page引导选项卡在另一页上打开选项卡
【发布时间】:2013-10-10 09:17:47
【问题描述】:

我的页面上有引导选项卡,它们链接到该页面上的正确内容区域。

当您离开该页面时,我在顶部有相同的选项卡,我想在右侧选项卡打开的情况下将它们引导回上一页。

这是我的标签在外部页面上的样子

 <ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
  <li><a href="page.php#submitted">Submitted</a></li>
  <li class="active"><a href="page.php#approved" data-toggle="tab">Approved</a></li>
  <li><a href="page.php#rejected">Rejected</a></li>
  <li><a href="page.php#uploaded">Uploaded</a></li>
 </ul>

如您所见,我已尝试链接到该页面并调用 id,该 id 转到正确的页面,但没有打开该选项卡。

我也尝试过在 jquery 中弄乱它,但没有足够有效的东西来显示。任何帮助将不胜感激!

编辑:

另一个页面上的标签看起来像这样。只是基本的引导选项卡。

<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
  <li class="active"><a href="#submitted" data-toggle="tab">Submitted</a></li>
  <li><a href="#approved" data-toggle="tab">Approved</a></li>
  <li><a href="#rejected" data-toggle="tab">Rejected</a></li>
  <li><a href="#uploaded" data-toggle="tab">Uploaded</a></li>
 </ul>
 <div id="my-tab-content" class="tab-content">
   <div class="tab-pane active" id="submitted">
   </div>
   <div class="tab-pane" id="approved">
   </div>
   <div class="tab-pane" id="rejected">
   </div>
   <div class="tab-pane" id="uploaded">
   </div>
  </div>

【问题讨论】:

  • 你能告诉我们每一页的完整代码吗?谢谢!
  • 这些页面非常长,由于保密协议,我的公司不允许这样做。我只能分享我可以轻松更改以保护它们的位和部分。
  • 嗯,这就是我的意思,我需要复制标签等。我不需要实际的完整页面或其他任何东西。
  • 刚刚更新了其他页面的标签
  • 酷谢谢。我会检查并尝试回答。

标签: jquery twitter-bootstrap


【解决方案1】:

我最终在这方面做了更多工作,并想出了这个选择正确的标签并打开正确的内容面板

  //grabs the hash tag from the url
  var hash = window.location.hash;
  //checks whether or not the hash tag is set
  if (hash != "") {
    //removes all active classes from tabs
    $('#tabs li').each(function() {
      $(this).removeClass('active');
    });
    $('#my-tab-content div').each(function() {
      $(this).removeClass('active');
    });
    //this will add the active class on the hashtagged value
    var link = "";
    $('#tabs li').each(function() {
      link = $(this).find('a').attr('href');
      if (link == hash) {
        $(this).addClass('active');
      }
    });
    $('#my-tab-content div').each(function() {
      link = $(this).attr('id');
      if ('#'+link == hash) {
        $(this).addClass('active');
      }
    });
  }

感谢 willbeeler 的良好开端! :)

【讨论】:

  • 是的,我打算添加最后一部分。好吧,很高兴看到你运行它。对不起,我没有尽快回到那里为你完成它。稍后再谈。
  • 如果此代码不起作用,请检查您的代码。我也没有工作。但我看到我的代码,我的代码不是#tabs li,而是#tabs ul。所以请为您的代码修改它。
【解决方案2】:

这可以通过利用 tab('show') 大大简化。

$(document).ready(function() {
   var hash = window.location.hash;

   if (hash != "")
       $('#tabs a[href="' + hash + '"]').tab('show');
   else
       $('#tabs a:first').tab('show');
});

【讨论】:

    【解决方案3】:

    我认为这也可以。我对其进行了测试,它对我有用:)

    $(document).ready(function() {
       var hash = window.location.hash;
       hash && $('ul.nav a[href="' + hash + '"]').tab('show');
    });
    

    【讨论】:

      【解决方案4】:

      好的,我对此进行了测试,它对我有用。在调用 jQuery 之后的某个位置添加以下代码。这应该在 page.php(外部页面链接到的主页面)上进行。让我知道这是否有帮助。

      $( document ).ready(function() {
        //grabs the hash tag from the url
        var hash = window.location.hash;
        //checks whether or not the hash tag is set
        if (hash != "") {
          //removes all active classes from tabs
          $('#tabs li').each(function() {
            $(this).removeClass('active');
          });
          //this will add the active class on the hashtagged value
          var link = "";
          $('#tabs li').each(function() {
            link = $(this).find('a').attr('href');
            if (link == hash) {
              $(this).addClass('active');
            }
          });
        }
      
      });
      

      编辑:顺便说一句,我需要为以下堆栈溢出问题提供一些支持:Getting URL hash location, and using it in jQuery 这就是我找到哈希变量的第一行。

      【讨论】:

      • 这似乎突出显示了正确的选项卡,但没有显示正确的内容。它只是显示默认内容。
      • 好的,我明白了。一秒。
      • 我正在处理这个问题。但我会在一个小时后回来修复它并提交给你。谢谢。
      【解决方案5】:

      对我有用的一点修改:

      <script>
      $( document ).ready(function() {
      //grabs the hash tag from the url
      var hash = window.location.hash;
      //checks whether or not the hash tag is set
      if (hash != "") {
      //removes all active classes from tabs
      $('#tabs li').each(function() {
      $(this).removeClass('active');
      });
      $('#myTabContent div').each(function() {
        $(this).removeClass('in active');
      });
      //this will add the active class on the hashtagged value
      var link = "";
      $('#tabs li').each(function() {
        link = $(this).find('a').attr('href');
        if (link == hash) {
          $(this).addClass('active');
        }
      });
      $('#myTabContent div').each(function() {
      
        link = $(this).attr('id');
        if ('#'+link == hash) {
      
          $(this).addClass('in active');
        }
      });
      }
      });
      </script>
      

      标签部分:

      <ul id="tabs" class="nav navbar-nav">
         <li class="active"><a data-toggle="tab" href="#homeTab">Home</a></li>
         <li><a data-toggle="tab" href="#accountTab">Accounts</a></li>
      </ul>
      

      标签内容部分:

      <div id="myTabContent" class="tab-content">
         <div class="tab-pane fade in active" id="homeTab">
         </div>
         <div class="tab-pane fade" id="accountTab">
         </div>
      </div>
      

      外部链接中使用的网址:

      <a href="/YourPageName#YourTabName">Account</<a>
      

      在我的情况下,网址看起来像:

       <a href="/IndexPage#accountTab">Account</a>
      

      【讨论】:

      • 我能问一下这与发布的答案有何不同吗?我只是并排看他们,但看不出有什么区别......
      • 如果您检查 div id ="homeTab" 的类,它会显示 class="tab-pane fade in active" 其中“in active”表示活动。如果您删除“in”,则在我的情况下不会显示 div 的内容,或者如果您使用 $(this).addClass('active');然后从其他页面单击选项卡后也不显示内容,它只显示导航栏。
      • 以上代码在我的情况下工作得很好。我已经从你的帖子中复制粘贴了代码,但在我的情况下不起作用,所以我已经根据我的工作情况对其进行了调试和修改
      • 所以真正改变的只是 id 名称和不同的 html,而不是 jQuery 保持不变?
      • 在 jquery 中更改行:$(this).removeClass('active');到 $(this).removeClass('in active');和 $(this).addClass('active');到 $(this).addClass('in active');并将导航栏内容部分中的类更改为
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2017-01-05
      • 1970-01-01
      • 2010-10-30
      • 2018-05-07
      • 1970-01-01
      相关资源
      最近更新 更多