jQuery - 添加和删除类

时间:2012-08-24 03:56:19

标签: javascript jquery html

我正在尝试将其设置为有一个默认选项卡,当单击另一个选项卡时,它会将该类从该选项卡中移除到所单击的选项卡上。

这是我的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');
});
});

2 个答案:

答案 0 :(得分: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>元素包装表会使您的文档无效。

答案 1 :(得分:1)

我认为你可以简化它;

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

您的HTML也需要一些认真的工作。为什么空表?

相关问题