2010-05-20 104 views
8

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

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

回答

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

完全未经测试和更麻烦一点,但它可能工作。

+0

'mouseOver'可能需要'mouseover' – 2014-09-18 21:25:09

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