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

8

是否可以使用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");
});
2个回答

3
不。最好在CSS中给该状态另外添加一个类,然后使用您的方法添加该类。
.regStyle:hover,
.regStyle.hover {
    css: properties;
    go: here;
}

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

编辑:好的,我撤回之前的说法。使用.trigger('mouseover')可能是一种方法。解释一下:

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

$('.otherElement').click( function() {
    $('.regStyle').trigger('mouseover');
});

完全没有经过测试,有些繁琐,但可能会起作用。


mouseOver might need to be mouseover - Kevin Reilly

0

看一下 http://api.jquery.com/hover/

.hover( handlerInOut )

    $( "li" ).hover(
      function() {
        $( this ).toggleClass('active');
      }
    );


.hover( handlerIn, handlerOut )

    $("li").hover(
      function() {
        $(this).addClass('active');
      }, function() {
        $(this).removeClass('active');
      }
    );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接