2012-08-24 72 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