开发者

How can I improve this click/toggle function in jquery?

开发者 https://www.devze.com 2023-01-03 07:05 出处:网络
$(\'.tabs a \').click(function () { var a = $(this).attr(\'href\'); if (a == \'#tab-1\') { $(\'.btn-buy\').hide();
$('.tabs a ').click(function () {
    var a = $(this).attr('href');
    if (a == '#tab-1') {
        $('.btn-buy').hide();
        $('.btn-sell').show();
    } else {
        $('.btn-sell').hide();
        $('.btn-buy').show();
    }
    return false;
});

... it works, but the code is ug开发者_如何学编程ly, too many lines. Can it be reduced any further?

Thanks in advance for your help!


You could just use toggle:

$(".tabs a").click(function() {
    $(".btn-buy").toggle();
    $(".btn-sell").toggle();
});

This would assume they start out in their correct state initially...

0

精彩评论

暂无评论...
验证码 换一张
取 消