2014-09-02 110 views
0

我有以下代码设置来清除其他标签点击时的类。它的建立在wordpress中,所以可能有一些wordpress代码在那里。基本上我想实现的是当你点击tab1时,tab2的类会清除,反之亦然,当你点击tab1时,tab1应该接收一个类。我相信我的代码应该可以工作,但它没有。有谁知道我做错了什么?onClick清除类功能

      <ul class="tab-links"> 
           <?php if(get_field('tab_1_-_naam', $id)): ?> 
            <li class="active1"><a onclick="$(".active2").hide();" href="#<? echo $slug; ?>_tab_<?php echo $id; ?>-1"><?php the_field('tab_1_-_naam', $id); ?></a></li> 
           <?php endif; ?> 


           <?php if(get_field('tab_2_-_naam', $id)): ?> 
            <li class="active2"><a onclick="setColor(this,'#FFF');". "this.style.color='#5D3B08!important';" href="#<? echo $slug; ?>_tab_<?php echo $id; ?>-2"><?php the_field('tab_2_-_naam', $id); ?></a></li> 
           <?php endif; ?> 
          </ul> 
<script type="text/javascript"> 
    $(function(){ 
    $(".active2").hide(); 
    }); 
</script> 

回答

1

当你点击一个链接就可以从你的父母消除了阶级兄弟姐妹(在<li>),然后添加一个类你自己的父(也是<li>)。

<script type="text/javascript"> 
    $(function(){ 
    $(".active2").hide(); 
    $(".tab-links a").click(function(){ 
     $(this).parent().siblings().removeClass('active'); 
     $(this).parent().addClass('active'); 
    }); 
    }); 
</script> 
+0

非常感谢这正是我需要的。 – 2014-09-02 09:23:18

1
 <script type="text/javascript"> 
    $(function(){ 
     $('.active1 a').click(function(e){ 
      //make tabs2 inactive 
      $('.active2').removeClass('active'); 
      // make tab1 active 
      $(.active1).addClass('active');  
     }); 

     $('.active2 a').click(function(e){ 
     //make tabs1 inactive 
     $('.active1').removeClass('active'); 
     // make tab2 active 
     $(.active2).addClass('active');  
    }); 
    }); 
</script> 
And In **active** class add css for active tab.