2012-08-24 196 views
0

我試圖讓它有一個默認選項卡,當另一個被點擊時,它將該類從該選項卡移到被點擊的類上。jQuery - 添加和刪除類

下面是我有HTML:

<div id="rightWrap"> 
    <div id="headStep"><span class="titleSub">Workers</span></div> 
    <center> 
    <br> 
    <a href="../about/?s=architect"><table class="highlight"> 
    </table></a> 
    <br> 
    <a href="../about/?s=broker"><table class=""> 
    </table></a> 
    <br> 
    <a href="../about/?s=operator"><table class=""> 
    </table></a> 
    <br> 
    <a href="../about/?s=consultant"><table class=""> 
    </table></a> 
    </center> 
</div> 

和JavaScript:

$(document).ready(function() { 
    $('#rightWrap a table').click(function(){ 
    $(this).addClass('highlight').siblings().removeClass('highlight'); 
}); 
}); 
+1

爲什麼在錨標籤內表? – nhahtdh

+0

非常討厭的標記 – mplungjan

回答

1

我想你可以簡化它到這個;

$(document).ready(function() { 
    $('#rightWrap a table').click(function(){ 
     $('#rightWrap').find('.highlight').removeClass('highlight'); 
     $(this).addClass('highlight');  
    }); 
}); 

您的HTML也需要一些認真的工作。爲什麼是空表?

4

你的表沒有兄弟姐妹。

$(document).ready(function() { 
    $('#rightWrap a table').click(function(){ 
     var $this = $(this); 
     $this.parent().siblings('a').find('table').removeClass('highlight'); 
     $this.addClass('highlight');  
    }); 
}); 

注意,如果網頁的DOCTYPE是不是HTML5,與<a>元素包裹表,使您的文件無效。

+0

@JohnConde感謝編輯,但編輯後的代碼與原始代碼沒有區別。 – undefined

+0

@ jfriend00是的,我知道它在HTML5中是合法的,我認爲table標籤有它們的標記,並且OP爲了簡潔沒有公佈它們。 – undefined