2014-09-02 105 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.