0

我正在尝试编写一个 JQuery If 语句。我想要实现的基本上是在单击某个 div(信息选项卡)时突出显示相应的链接(a)。如您所见,它们都被隐藏了,但是当单击时,它们会以漂亮的淡入淡出可见。我想突出显示被点击的项目。(将背景颜色更改为我想要的任何颜色,例如下面代码中的红色。)

我下面的代码有效,但不正确。它突出显示该 div 中的所有 a。我只想突出显示被点击的那个。谢谢你们的帮助,你们很棒。

$(document).ready(function () {
    $('#infotab_two_s, #infotab_three_s, #infotab_four_s, #infotab_five_s').hide();
});

$('.subnav_holster li').click(function () {
    var Vinfotab = this.id + '_s';
    $('.infotab:visible').fadeOut('fast', function () {
        $('#' + Vinfotab).fadeIn('fast');
        var Vinfotab_selected = 'Vinfotab:visible';
        $("subnav_holster li a").css({
            "color": "red"
        });
    });
});
4

2 回答 2

2

抓住li被点击的元素并访问该元素的a

$('.subnav_holster li').click(function () {
    var Vinfotab = this.id + '_s';
    var clicked = $(this);
    $('.infotab:visible').fadeOut('fast', function () {
        $('#' + Vinfotab).fadeIn('fast');
        var Vinfotab_selected = 'Vinfotab:visible';
        clicked.find('a').css({
            "color": "red"
        });
    });
});
于 2011-03-26T03:38:06.313 回答
1

您应该缓存this然后突出显示它:

$('.subnav_holster li').click(function () {
    var Vinfotab = this.id + '_s',
        $this = $(this);
    $('.infotab:visible').fadeOut('fast', function () {
        $('#' + Vinfotab).fadeIn('fast');
        var Vinfotab_selected = 'Vinfotab:visible';
        $('.subnav_holster li a').css({
            "background-color": "white" // reset all to default color
        });
        $this.find('a').css({
            "background-color": "red"   // set highlight to this element only
        });
    });
});
于 2011-03-26T03:39:55.310 回答