从另一个页面访问 div 选项卡



我正在使用twitters bootstrap的一些代码,一种导航栏选项卡。

这些选项卡似乎是使用 #tab 功能检索的。假设此菜单位于名为 index 的文件中.php

<div class="bs-docs-example">
    <div class="tabbable tabs-left">
      <ul class="nav nav-tabs">
        <li class="active"><a href="#tab1" data-toggle="tab">Section 1</a></li>
        <li><a href="#tab2" data-toggle="tab">Section 2</a></li>
        <li><a href="#tab3" data-toggle="tab">Section 3</a></li>
      </ul>
      <div class="tab-content">
        <div class="tab-pane active" id="tab1">
          <p>I'm in Section A.</p>
        </div>
        <div class="tab-pane" id="tab2">
          <p>Howdy, I'm in Section B.</p>
        </div>
        <div class="tab-pane" id="tab3">
          <p>What up girl, this is Section C.</p>
        </div>
      </div>
    </div>
  </div>

但是我如何从另一个页面(如 main.php)直接跳转到 tab2? 你明白我的意思吗?我正在尝试:

<a href="index.php#tab2">link to tab2</a>

<a href="index.php#tab2" data-toggle="tab">link to tab2</a>

但它不起作用...,不知道是否需要以某种方式使用数据切换="选项卡"?

任何提示赞赏!

使用 jquery easytabs 插件可以非常容易地解决这个问题。阅读更多并下载插件: http://os.alfajango.com/easytabs/记得安装 jquery 和其他 Javascript

.HTML:

<head> <!-- other markup -->
   <script src="/javascripts/jquery.js" type="text/javascript"></script>
   <script src="/javascripts/jquery.hashchange.js" type="text/javascript"></script>
   <script src="/javascripts/jquery.easytabs.js" type="text/javascript"></script>
   <script type="text/javascript">
       $(function() {
             $('#tab-container').easytabs();
       });
   </script>
</head>
<body>

<div id="tab-container" class="tab-container">
<ul class='etabs'>
  <li class='tab'><a href="#tabs1-html">HTML Markup</a></li>
  <li class='tab'><a href="#tabs1-js">Required JS</a></li>
  <li class='tab'><a href="#tabs1-css">Example CSS</a></li>
</ul>
<div id="tabs1-html">
  <h2>HTML Markup for these tabs</h2>
  <!-- content -->
</div>
<div id="tabs1-js">
  <h2>JS for these tabs</h2>
  <!-- content -->
</div>
<div id="tabs1-css">
  <h2>CSS Styles for these tabs</h2>
  <!-- content -->
</div>

.CSS:

.etabs { margin: 0; padding: 0; }
.tab { display: inline-block; zoom:1; *display:inline; background: #eee; border: solid 1px #999; border-bottom: none; -moz-border-radius: 4px 4px 0 0; -webkit-border-radius: 4px 4px 0 0; }
.tab a { font-size: 14px; line-height: 2em; display: block; padding: 0 10px; outline: none; }
.tab a:hover { text-decoration: underline; }
.tab.active { background: #fff; padding-top: 6px; position: relative; top: 1px; border-color: #666; }
.tab a.active { font-weight: bold; }
.tab-container .panel-container { background: #fff; border: solid #666 1px; padding: 10px;    -moz-border-radius: 0 4px 4px 4px; -webkit-border-radius: 0 4px 4px 4px; }

使用 Bootstrap 和包含的 jQuery UI,导航哈希的实现非常简单。使用这样的 JavaScript

$(function () {
  var gotoTab = $('[href=' + location.hash + ']');
  gotoTab && gotoTab.tab('show');
});

但是,这不适用于历史记录或后退按钮。所以这是使用javascript进行选项卡导航的缺点。我不知道是否有其他解决方案?

最新更新