代码之家  ›  专栏  ›  技术社区  ›  Omar

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

  •  8
  • Omar  · 技术社区  · 15 年前

    是否可以(使用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 回复  |  直到 15 年前
        1
  •  3
  •   Soren    10 年前

    不,最好在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');
    });
    

    完全未经测试,有点麻烦,但它可能会工作。

        2
  •  0
  •   mrks    11 年前

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