使用jQuery将元素样式设置为:hover样式

Oma*_*mar 8 css jquery

是否可以(使用jQuery或其他方式)将某个元素的:hover样式设置为样式表中定义的样式?

.regStyle {
   background-color: #000;
}

.regStyle:hover {
   background-color: #fff;
} 

Trying it out

$("#differentButton").click(function(){
    // this doesn't work 
    $("#someElement").addClass("regStyle:hover").remove("regStyle");
});
Run Code Online (Sandbox Code Playgroud)

dcl*_*901 3

不。最好在 CSS 中为该状态提供另一个类本身,然后使用您的方法添加该类。

.regStyle:hover,
.regStyle.hover {
    css: properties;
    go: here;
}

$("#differentButton").click(function(){
    // this doesn't work 
    $("#someElement").addClass("hover");
});
Run Code Online (Sandbox Code Playgroud)

编辑:好的,我收回它。可能有一种方法.trigger('mouseover')。解释:

$('.regStyle').mouseover( function() {
    $(this).css('css','property');
});

$('.otherElement').click( function() {
    $('.regStyle').trigger('mouseover');
});
Run Code Online (Sandbox Code Playgroud)

完全未经测试并且有点麻烦,但它可能有效。