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

时间:2015-01-25 21:51:01

标签: jquery css jquery-hover

我有一个复杂的问题。

这里有两个要素:

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

这部分我已经完成,使用.data()

第二部分是棘手的部分:

(2)。在同一个<a>中选择<li>时,我希望颜色保持不变,并应用下划线。所以我想要#34;世界大会&#34;文本保持绿色,加下划线,并使<li>的其余部分为白色,未激活的颜色。

有没有办法在悬停功能中使用回调来执行此操作?

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

我只是徘徊在$(&#34; .steps li a&#34;)但这不起作用,因为第一部分工作,必须删除该类。

无论如何,我对此不确定。任何意见,将不胜感激。

代码如下:

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'));
     });

2 个答案:

答案 0 :(得分:2)

以下是您问题的几种解决方案(我使用了一些十六进制值,因为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;
}

答案 1 :(得分:1)

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

然后,一组新的规则可以涵盖li和基于类

的颜色
$(".steps li a").hover(function){
     $(this).parent().toggleClass('aHovered');
});

.steps li.aHovered{
  color : white
}

.steps li.aHovered a{
  color : green
}
相关问题