根据悬停事件中的ID和类永久更改颜色

时间:2014-08-11 14:53:03

标签: jquery css

我需要一个jQuery代码,当我将鼠标悬停在它上面时,会根据div的类或ID永久更改文本的颜色。所以当我停止在div上停留时,文本颜色仍然会改变。

jQuery的:

$("#notam").hover(function () {
    $("#notam").addClass("fboto");
    },
    function () {
        $("#notam").addClass("fboto");
});

HTML:

<div id='bulletin'>
    <div class='notesColor1'>
        notes1
    </div>
    <div class='notesColor1'>
        notes2
    </div>
    <div class='notesColor1'>
        notes3
    </div>    
</div>

CSS:

.notesColor{
    background-color: #CEDEFF;  
}

.notesColor1{
    background-color: #ADC4ED;  
}

My JSFiddle

2 个答案:

答案 0 :(得分:3)

要根据下面的评论在jQuery中执行此操作,您需要做的就是在mouseover事件上分配一个类:

 $(".notesColor1").on('mouseover', function () {
     $(this).addClass("hover");
 });

Demo Fiddle

答案 1 :(得分:0)

要获得此行为,您需要使用鼠标悬停而不是悬停。
这是一个简单的示例:
http://jsfiddle.net/Lakvo3dm/ HTML:

export class Cell {
    constructor(
        public name: string = "",
        public rowspan: number = 1,
        public colspan: number = 1
    ){ }
}

export class Table {
    public c1 = new Cell()
    public c2 = new Cell()
    public c3 = new Cell()
    
}

CSS:

<button class="cell white"></button>

jQuery

.white {
    background-color: #ffffff;
    border: 1px solid #d3d3d3;
}
.black {
    background-color: black;
}
.cell {
    width: 10px;
    height: 15px;
}