【问题标题】:Get links to stay active获取链接以保持活跃
【发布时间】:2023-03-14 17:17:03
【问题描述】:

我有 4 个选项,当单击它们时,单击的选项变为焦点并且其背景颜色/文本颜色发生变化,而所有其他选项都恢复为正常状态。我有一些代码可以更改字体颜色,但我不知道如何更改 div 颜色。我也在尝试用 CSS 和这段代码弄清楚如何在页面加载时默认突出显示其中一个选项。

Jquery-最后一行“($("a")”是更改字体颜色的代码行;上面的代码与我在页面上的过滤系统有关。

$(function () {
         var $panels = $( '#image-wrapper .panel' );

        $( '#category > div > a' ).on( 'click', function (event) {
            event.preventDefault();

            var categoryToShow = $( this ).data( 'filter' );
            $panels.addClass( 'active' ).filter( '.' + categoryToShow ).removeClass( 'active' );
            $("a").addClass("active-color").not(this).removeClass("active-color");
            /*$("#category .current-div").addClass("active-bg").not(this).removeClass("active-bg");*/
        } );
    });

HTML

<div id="category">
            <div class="all-div current-div">
                <a href="#" data-filter="all"><h2>ALL</h2></a>
            </div>
            <div class="ed-div current-div">
                <a href="#" data-filter="ed"><h2>EDITORIAL</h2></a>
            </div>
            <div class="pr-div current-div">
                <a href="#" data-filter="pr"><h2>PR</h2></a>
            </div>
            <div class="cr-div current-div">
                <a href="#" data-filter="cr"><h2>CREATIVE</h2></a>
            </div>
        </div>

CSS

#content, 
#category {
    overflow: hidden;
}

#category div {
    float: left;
    margin: 40px 0 0 0;
    height: 100px;
    width: 240px;
    background-color: #F5F5F5;
}

#category .all-div {
    background-color: #000000;
}
#category .all-div a {
    color: #ffffff;   
}

.active-color {
    color: #000000;
    background-color: green;
}
.active-bg {
    background-color: green;
}

【问题讨论】:

  • 使用this.parent()方法怎么样

标签: javascript jquery css hyperlink highlight


【解决方案1】:

好吧,.on 在您的选择器上不起作用,所以我将其更改为 .live 并将您的背景选择器更改为 .current-div,因为您的 a 标签需要 display: block 以具有可见的背景。

检查一下:

http://jsfiddle.net/A3n7S/15/

$(function () {

     var $panels = $( '#image-wrapper .panel' );

     $(".current-div").not('.all-div').first().addClass("active-color")
     $( '#category > div > a' ).live( 'click', function (event) {
        event.preventDefault();  
        var categoryToShow = $( this ).data( 'filter' );
        $panels.addClass( 'active' ).filter( '.' + categoryToShow ).removeClass( 'active' );
        $(".current-div").not('.all-div').removeClass("active-color");
        $(this).parent().addClass("active-color");

      /*$("#category .current-div").addClass("active-bg").not(this).removeClass("active-bg");*/
    } );
});

替换那个:

.active-color {
   color: #000000;
   background-color: green;
}

与:

 #category .active-color {
   color: #000000;
   background-color: green;
 }

【讨论】:

  • 好的,我更改了代码以反映我对您想要的结果的看法
  • 是的,就是这样,除了我试图让黑色 div 在单击新链接后恢复为灰色(因此它类似于其他 3 个非活动链接);这只是默认的起始位置。我只是在 CSS 中这样做,因为我不知道该怎么做。
  • 好的,摆弄你的代码我想出了如何让它做我想做的事。谢谢一堆。我现在唯一需要做的就是弄清楚如何让其中一个 div 开始聚焦。
  • 是的!除了 "$(".current-div").removeClass('all-div active-color');" 我想通了。惊人的。非常感谢。
  • 另一个快速的问题 - 我的图像在单击其中一个过滤器时变得透明,为什么它在 IE7/8 中不起作用?
猜你喜欢
  • 2011-05-21
  • 2011-08-07
  • 2020-12-06
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多