html页面:

<section>
 <h2>Section Title</h2>
 <ul class="tab-nav">
  <li><a href="#tab1" title="">Tab 1</a></li>
  <li><a href="#tab2" title="">Tab 2</a></li>
 </ul>
 <div class="tab-contents">
  <div ><!-- Tab 1 content here --></div>
  <div ><!-- Tab 2 content here --></div>
 </div>
</section>

js:

var tabs = {
 init: function(){
  var $tab_contents=$('.tab-contents'), $tab_nav=$('.tab-nav');
  $tab_contents.find('.tab-content:not(:first)').hide();
  $tab_nav.find('li:first').addClass('active');
  $tab_nav.on('click', 'li a', function(e){
   e.preventDefault();
   var $this=$(this),activeTab=this.hash,parent=$this.parents('section'),$contents=$(parent,$tab_contents);
//this.hash 获取锚链接<a href="#tab1"></a> $(parent,$tab_nav).find('li').removeClass('active'); $this.parent().addClass('active'); $contents.find('.tab-content').hide(); $contents.find(activeTab).fadeIn(250); }); } }; $(document).ready(tabs.init());

  

 

相关文章:

  • 2022-12-23
  • 2019-11-13
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-08
  • 2022-01-16
  • 2022-03-01
  • 2021-07-23
相关资源
相似解决方案