简体   繁体   English

#id 在页面 URL 中时未显示选项卡

[英]Tabs not shown when #id is in page URL

I tried to add a tabs option to my page URL so that the specified tab is focused when the page loads我试图在我的页面 URL 中添加一个选项卡选项,以便在页面加载时指定的选项卡聚焦

Example: mywebsite/page/#tab3示例:mywebsite/page/#tab3

I'm using jQuery, but not jQuery UI.我正在使用 jQuery,但不是 jQuery UI。

Any ideas om how I can do it?有什么想法我该怎么做?

<div class="wrapper">

<ul class="tabs clearfix" data-tabgroup="first-tab-group">
  <li><a href="#tab1" class="active">Tab 1</a></li>
  <li><a href="#tab2">Tab 2</a></li>
  <li><a href="#tab3">Tab 3</a></li>
</ul>

<section id="first-tab-group" class="tabgroup">
  <div id="tab1">
    <h2>Heading 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla deserunt consectetur ratione id tempore laborum laudantium facilis reprehenderit beatae dolores ipsum nesciunt alias iusto dicta eius itaque blanditiis modi velit.</p>
  </div>
  <div id="tab2">
    <h2>Heading 2</h2>
    <p>Adipisci autem obcaecati velit natus quos beatae explicabo at tempora minima voluptates deserunt eum consectetur reiciendis placeat dolorem repellat in nam asperiores impedit voluptas iure repellendus unde eveniet accusamus ex.</p>
  </div>
  <div id="tab3">
    <h2>Heading 3</h2>
    <p>Atque ratione soluta laboriosam illo inventore amet ipsum aliquam assumenda harum provident nam accusantium neque debitis obcaecati maxime officia saepe ad ducimus in quam libero vero quasi. Saepe sit nisi?</p>
  </div>
</section>

</div>
$('.tabgroup > div').hide();
$('.tabgroup > div:first-of-type').show();
$('.tabs a').click(function(e){
  e.preventDefault();
    var $this = $(this),
        tabgroup = '#'+$this.parents('.tabs').data('tabgroup'),
        others = $this.closest('li').siblings().children('a'),
        target = $this.attr('href');
    others.removeClass('active');
    $this.addClass('active');
    $(tabgroup).children('div').hide();
    $(target).show();
})    
$(document).ready(function(){
  if (document.location.hash != '') {
    $(".active").removeClass("active");
    $('a[href*="' + document.location.hash + '"]').addClass("active");
    $('[id^=tab]').hide();
    $(document.location.hash).show();
  }
});

A simple approach is to use filter() to return tab that matches the url hash and trigger a click on it.一个简单的方法是使用filter()返回与 url hash 匹配的选项卡并触发点击它。

$('.tabs a').filter(function(){ return this.hash === location.hash; }).click();

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

相关问题 显示某些选项卡时如何隐藏元素 - How to hide an element when certain tabs is shown 在路由组件中使用 privateroute 时,来自 url 的 id 显示为未定义 - id from url is shown undefined when used privateroute inside route component 在上面的脚本中,我应该在哪里插入setTimeout()? 由于var id =“#dialog”; 是加载页面时显示的div - where do i insert setTimeout() in the above script? since var id=“#dialog”; is the div that is shown when the page is loaded 当 URL 具有导航标签 ID 时,我想添加和删除 class - I want to add and remove class when URL having nav tabs id jQuery UI选项卡-当URL重定向到特定选项卡时,页面将关闭 - jQuery UI tabs - page goes down when the URL is redirected to a particular tab jQuery Ui选项卡URL问题与页面基础 - Jquery Ui tabs url problem with page base 显示和隐藏jQuery选项卡 - jQuery tabs are shown and hidden 隐藏溢出和网址中的ID。 未显示内容,跳至顶部 - Overflow hidden and ID in URL. Content not shown, jumps on top 通过直接URL链接定位页面上的定位标签会中断标签点击时的URL - Targeting tabs on page with direct URL link breaks URL on tab click 路由不匹配时,未显示未找到页面 - not found page is not shown when route is not matched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM