jquery un-hover

前端 未结 4 1403
花落未央
花落未央 2021-02-07 21:08

I have this script to cause a background color on a paragraph on hover of link within the paragraph. What I don\'t know how to do is cause it to return to the original backgroun

相关标签:
4条回答
  • 2021-02-07 21:47

    The function below works as onmouseover and onmouseout

    $(function () {
        $(".box a").hover(function () {
            $(this).parent().css('background-color', '#fff200');
        }, function () {
            // change to any color that was previously used.
            $(this).parent().css('background-color', '#fff200');
        });
    });
    
    0 讨论(0)
  • 2021-02-07 21:53

    There is a hover out handler in the jQuery documentation. That's where you'd want to return the color to the original. If all you are doing is changing color, why not use CSS?

    $(function(){
        $(".box a").hover(function(){
            $(this).parent().css('background-color', '#fff200');
        },function(){
            $(this).parent().css('background-color', '#originalhexcolor');
        });
    });
    
    0 讨论(0)
  • 2021-02-07 21:57

    JQuery

    $(".box a").hover(function(){
        $(this).parent().css('background-color', '#fff200');
     }, function() {
         $(this).parent().css('background-color', '#ffffff');
     });
    

    See fiddle.

    0 讨论(0)
  • 2021-02-07 22:08

    If you must use jQuery for this, use addClass() rather than css():

    $('.box a').hover(function(){
        $(this).closest('.box').addClass('hoveredOver');
    }, function(){
        $(this).closest('.box').removeClass('hoveredOver');
    });
    

    With the CSS:

    .hoveredOver {
        background-color: #fff;
    }
    

    JS Fiddle demo.

    References:

    • addClass().
    • removeClass().
    0 讨论(0)
提交回复
热议问题