JQuery addClass()和removeClass()同步

时间:2012-06-15 09:40:49

标签: jquery css

我有下表:

<table>
<tr class="change"><td>Click to change</td></tr>

<tbody id="p1" class="now">
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
</tbody>
<tbody id="p2" class="next">
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
</tbody>
<tbody id="p3" class="previous">
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
</tbody>
</table>

我想用“现在”类隐藏tbody,当我点击更改行时,显示带有“next”类的那个。

我的Jquery(仅在现在和下一次计算):

$(document).ready(function(){

$('.change').click(function(){

    $('.now').hide('slow', function(){
        $('.next').show('slow', function(){     


        $('#p1').removeClass('recent');
        $('#p2').removeClass('next');

        $('#p1').addClass('next');
        $('#p2').addClass('recent'); 

        });
    });

});  

});

我看到我做错了所以我想问你,如何以某种方式很好地同步它,当我点击“更改”时我的“现在”tbody变为“previous”,“next”变为“现在”和“上一个”成为下一个?

3 个答案:

答案 0 :(得分:3)

$(document).ready(function () {
    $('.change').click(function () {
        $('.now').hide('slow', function () {
            $('.next').show('slow', function () {
                $prev = $('.previous');
                $now = $('.now');
                $next = $('.next');
                $prev.removeClass('previous').addClass('next');             
                $now.removeClass('now').addClass('previous');
                $next.removeClass('next').addClass('now');

            });
        });
    });
});

答案 1 :(得分:1)

$('.change').click(function(){
    var now = $('.now');
    var next = $('.next');
    var previous = $('.previous');
    now.hide('slow', function(){
        next.show('slow');
        previous.removeClass('previous').addClass('next');             
        now.removeClass('now').addClass('previous');
        next.removeClass('next').addClass('now');
    });
});​

DEMO

答案 2 :(得分:-1)

$('.change').click(function() {
    $('.now').attr('class', 'previous');
    $('.next').attr('class', 'now');
    $('.previous').attr('class', 'next');
});