【问题标题】:Modifying An Element Already Activated By Another Selector修改已被另一个选择器激活的元素
【发布时间】:2015-01-25 21:51:01
【问题描述】:

我有一个复杂的问题。

这里有两个元素:

(1). 选择$(".steps li") 时,我希望整个<li> 将颜色更改为rgb(66, 81, 95)。然后它必须变回原来的样子。

这部分我已经用.data()完成了。

第二部分是棘手的部分:

(2). 在相同的<li> 中选择<a> 时,我希望颜色保持不变并应用下划线。所以我希望“世界大会”文本保持绿色,加下划线,并让<li> 的其余部分为白色,未激活的颜色。

有没有办法在悬停函数中使用回调来做到这一点?

我需要 (1) 和 (2) 同时工作。

我厌倦了只将鼠标悬停在 $(".steps li a") 上,但这不起作用,因为要使第一部分起作用,必须删除该类。

无论如何,我不确定这一点。任何意见,将不胜感激。

代码如下:

CSS:

    html, body {
    background: #000;
    color: #e7e7e7;
    font-family:"Helvetica", "Arial", "Bitstream Vera Sans", "Verdana", sans-serif;
    margin:0;
    padding:0;
}
a {
    color: rgb(66, 81, 95);
    text-decoration:none;
}
a:hover {
    /*color:#a0a0a0;*/
    text-decoration:none;
}
.wa a {
    color: rgb(68, 118, 67);
}
.steps {
    width:400px;
    margin:0 auto;
    text-align:left;
    line-height: 200%;
}
.steps a:hover {
    text-decoration: underline;
}
.steps li:hover {
    cursor: pointer;
    color: rgb(66, 81, 95);
}

jQuery:

$(".steps li").hover(function () {
    var the_class = $(this).children().attr("class");
    $(this).data('class', the_class);
    console.log(the_class);
    $(this).children().toggleClass(the_class);
}, function () {
    $(this).children().attr("class", $(this).data('class'));
});

编辑:我实际上不得不两次使用$.data() 来解决这个问题,因为在我本地托管的代码中,我最终不得不在列表中添加更多的锚标记,所有这些标记都有自己的颜色。

它现在是这样工作的:

 $(".steps li").hover(function () {
    var the_class = $(this).children().attr("class");
    $(this).data('class', the_class);
    $(this).children().toggleClass(the_class);
}, function () {
  $(this).children().attr("class", $(this).data('class'));
});

$(".steps li a").hover(function(){
     $(this).parent().parent().toggleClass('notHover');
     $(this).parent().attr("class", $(this).parent().parent().data('class'));
     }, function()
     {
     $(this).parent().parent().toggleClass('notHover');
     $(this).parent().removeClass($(this).parent().parent().data('class'));
     });

【问题讨论】:

标签: jquery css jquery-hover


【解决方案1】:

这里有几个解决您问题的方法(我使用了一些十六进制值,因为 RGB 通常不用于网络)。

解决方案 1:添加/删除悬停类:JSFiddle

jQuery:

$(".steps li a").hover(function() {
    $(this).parent().toggleClass("color");
});

CSS:

.color {
    color: #e7e7e7 !important;
}

解决方案 2a:更改 jQuery:JSFiddle

jQuery:

$(".steps li a").hover(function() {
    $(this).parent().css("color", "#e7e7e7");
});
$(".steps li a, .steps li").mouseleave(function() {
    $(this).parent().css("color", "");
});

CSS:

.steps li:hover, .steps li:hover a {
    cursor: pointer;
    color: rgb(66, 81, 95);
}

.steps li a:hover {
    color: #447643;
}

解决方案 2b:再次更改 jQuery:JSFiddle

jQuery:

$(".steps li a").hover(function() {
    $(this).parent().attr("style", "color: #e7e7e7");
}, function() {
    $(this).parent().attr("style", "");
});

CSS:

.steps li:hover, .steps li:hover a {
    cursor: pointer;
    color: rgb(66, 81, 95);
}

.steps li a:hover {
    color: #447643;
}

【讨论】:

  • 在这一点上非常接近并且几乎可以接受,问题出在您的示例中,当我将鼠标悬停在“Word Assembly”文本上时,
  • 的其余部分也会改变颜色。理想情况下,这就是我不想要的。
  • @LSD 当<a> 悬停时,您希望非<a> 链接变回白色?
  • 是的,如果<li> 的另一部分(非<a> 部分)悬停,然后像您所做的那样更改整个事物的颜色。但如果<a> 部分被选中,则让<li> 的其余部分恢复为白色
  • @LSD 更新了我的帖子。它应该按照您现在的要求进行。使用此解决方案,您将不需要 !important hack。
  • @TylerH 需要在 mouseleave 上退出内联 css
  • 【解决方案2】:

    当<a> 悬停时,只需在父<li> 上切换一个类。

    那么一组新的规则就可以覆盖基于类的li和a颜色

    $(".steps li a").hover(function){
         $(this).parent().toggleClass('aHovered');
    });
    
    .steps li.aHovered{
      color : white
    }
    
    .steps li.aHovered a{
      color : green
    }
    

    【讨论】:

    • 在我将 !important 添加到 CSS 类 aHovered 之前,这不起作用:P 谢谢!
    • 是的,可能必须取消一些其他规则,但基础已经存在,或者使这些规则更具体
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签