修改已被另一个选择器激活的元素



我有一个有点复杂的问题。

这里有两个元素:

(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'));
     });
只需在

悬停<a>时切换父<li>上的类。

然后一组新的规则可以覆盖基于类的 li 和颜色

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

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

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

j查询:

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

.CSS:

.color {
    color: #e7e7e7 !important;
}

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

j查询:

$(".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

j查询:

$(".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;
}

最新更新