简体   繁体   English

如何在页面加载时基于锚标记在div之间切换?

[英]How do I toggle between divs based on anchor tag on page load?

I'm toggling between two divs that display pricing options. 我正在两个显示定价选项的divs之间切换。 It defaults to the first option on page load. 它默认为页面加载时的第一个选项。 Using an anchor link, how can I have the toggle launch option two? 使用锚链接,我如何才能使用切换启动选项2?

Here's the page for reference. 这是供参考的页面。 and a Fiddle of a condensed version to show the code I'm working with. 和一个缩小版的小提琴 ,以显示我正在使用的代码。 I want the "Club and Youth" pricing to show when #Club-Youth is anchored to the URL. 我希望“俱乐部和青年”定价在#Club-Youth锚定到URL时显示。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

HTML HTML

<div class="pricing-switcher">
  <a class="toggle active" id="HS-College">High School and College</a>
  <a class="toggle" id="Club-Youth">Club and Youth</a>
</div>

<div class="pricing-wrapper">
  <div class="panels HS-College">Option 1 Option 1 Option 1 Option 1 Option 1 Option 1 Option 1 Option 1 Option 1</div>
  <div class="panels Club-Youth hide">Option 2 Option 2 Option 2 Option 2 Option 2 Option 2 Option 2 Option 2 Option 2</div>
</div>

CSS CSS

.panels {
  display: flex;clear:both;
  padding-bottom: 30px;
}

.panels.HS-College {
  justify-content: center;
}

.panels.hide {
  display: none 
}

.pricing-switcher {
  float: left;
  width: 100%;
}

.toggle {
  float: left;
  display: flex;
  border: 2px solid #000;
  margin-right: 10px;
  cursor: pointer;
}

.toggle.active {
  background-color: red;
}

JS Toggle JS切换

$(document).ready(function(){
  $('.toggle').click(function(){
    var self = $(this);  
    $('.panels').addClass('hide');
    $('.toggle').removeClass('active');
    self.addClass('active');
    $('.panels.'+ self.attr('id')).removeClass('hide');
  });
});
$(window.location.hash).click();

window.location.hash will return #Club-Youth from the below url. window.location.hash将从以下网址返回#club-Youth。

https://www.hudl.com/products/assist/volleyball?token=K6~s9DTGVrSKh~X9VMetZv6YLKSMtAAp#Club-Youth https://www.hudl.com/products/assist/volleyball?token=K6~s9DTGVrSKh~X9VMetZv6YLKSMtAAp#Club-Youth

Using $(window.location.hash) will be the same as $('#Club-Youth') and fire the click event on that element. 使用$(window.location.hash)将与$('#Club-Youth')并触发该元素上的click事件。

Make sure this code is below your $('.toggle').click(function(){}) so it fires the code you have attached already. 确保此代码低于$('。toggle')。单击(function(){})以便它触发您已附加的代码。

Look for a hash value in the URL on page load 在页面加载时查找URL中的哈希值

var hash = window.location.hash;
if (!(typeof object === 'undefined')) {
   // show/hide based on value of hash
   if (hash=='Club-Youth') {
      // show panel 1
   } else {
      // show panel 2
   }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM