【问题标题】:Goto specific Tab (accordin) on # hyperlink转到 # 超链接上的特定选项卡 (accordin)
【发布时间】:2015-04-30 03:01:00
【问题描述】:

我正在使用以下代码来切换选项卡。但问题是如果我将标签 ID 放在带有 # 的 url 中,它不会转到那个特定的标签。

<a href="index.php#tab2">Tab 2</a>
<a href="index.php#tab3">Tab 3</a>

下面是代码

jQuery(document).ready(function() {
    jQuery('.tabs .tab-links a').on('click', function(e)  {
        var currentAttrValue = jQuery(this).attr('href');

        // Show/Hide Tabs
        jQuery('.tabs ' + currentAttrValue).show().siblings().hide();

        // Change/remove current tab to active
        jQuery(this).parent('li').addClass('active').siblings().removeClass('active');

        e.preventDefault();
    });
});

html代码

        <ul class="tab-links">
            <li class="active"><a style="" href="#tab1">Account</a></li>
            <li><a href="#tab2">Jobs Posted</a></li>
            <li><a href="#tab3">Messages</a></li>
            <li><a href="#tab4">Post A Job</a></li>
        </ul>
<div class="tab-content">
    <div id="tab1" class="tab active" style="padding-bottom:30px;">
    </div>
    <div id="tab1" class="tab" style="padding-bottom:30px;">
    </div>
    <div id="tab1" class="tab" style="padding-bottom:30px;">
    </div>
</div>

【问题讨论】:

  • 请为您要显示的元素添加 html。最好创建一个演示问题的 JSFiddle。
  • 你可以实现 jQuery UI 并使用 tabs() 方法,如下所示stackoverflow.com/questions/578348/…希望这会有所帮助
  • 您必须识别您使用 id 显示的元素 - 这就是浏览器如何知道如何将 url 中的哈希与要滚动到的元素相匹配。您还需要一些 jquery 来读取 url 并显示适当的元素并选择适当的选项卡
  • 我也添加了 html 代码。 @ekusela

标签: javascript jquery


【解决方案1】:

您可以尝试修改这一行:var currentAttrValue = jQuery(this).attr('href').split("#")[1];

这样您就获得了链接的href 属性的"tab2" 部分

jQuery(document).ready(function() {
    var changeTab = function(id) {
        // Show/Hide Tabs
        jQuery('.tab-content #' + id).show().siblings().hide();

        // Change/remove current tab to active
        jQuery('.tab-content #' + id + ':visible').addClass("active").siblings().removeClass('active');
    };

    jQuery('.tabs .tab-links a').on('click', function(e)  {
        e.preventDefault();
        // Here you will get the tab id, comming from the clicked hyperlink
        var currentAttrValue = jQuery(this).attr('href').split("#")[1];
        // Change/remove current tab to active
        jQuery(this).parents("li").addClass("active").siblings().removeClass('active');
        changeTab(currentAttrValue);
    });
  
    var tab = document.location.href.split("#");
    if(tab.length > 0) {
      var id = tab[1];
      jQuery('.tabs .tab-links a[href="#' + id + '"]').click();
      window.addEventListener("load",function() {
        jQuery(window).scrollTop(0);
      },true);
    }
});
.active {
  color:red;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tab-content">
<div id="tab1" class="tab active" style="padding-bottom:30px;">tab 1 content
</div>
<div id="tab2" class="tab" style="padding-bottom:30px;">tab 2 content
</div>
<div id="tab3" class="tab" style="padding-bottom:30px;">tab 3 content
</div>
</div>
<div class="tabs">
  
  <div class="tab-links">
    <a href="index.php#tab1">Tab 1</a>
    <a href="index.php#tab2">Tab 2</a>
    <a href="index.php#tab3">Tab 3</a>
  </div>
  
</div>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 2014-12-08
  • 1970-01-01
  • 2011-12-13
  • 2015-06-23
相关资源
最近更新 更多