How to: Add/Remove Class on mouseOver/mouseOut - JQuery .hover?

前端 未结 4 1382
自闭症患者
自闭症患者 2020-12-05 17:57

Looking to change the border color on a box..

..when the user mouses over/out..

Here\'s the attempted code.. Needs Work!

JQuery:

<         


        
相关标签:
4条回答
  • 2020-12-05 18:25

    You are missing the dot on the selector, and you can use toggleClass method on jquery:

    $(".result").hover(
      function () {
        $(this).toggleClass("result_hover")      
      }
    );
    
    0 讨论(0)
  • 2020-12-05 18:30

    You could just use: {in and out function callback}

    $(".result").hover(function () {
        $(this).toggleClass("result_hover");
     });
    

    For your example, better will be to use CSS pseudo class :hover: {no js/jquery needed}

    .result {
        height: 72px;
        width: 100%;
        border: 1px solid #000;
      }
      .result:hover {
        background-color: #000;
      }
    
    0 讨论(0)
  • 2020-12-05 18:31

    Your selector is missing a . and though you say you want to change the border-color - you're adding and removing a class that sets the background-color

    0 讨论(0)
  • 2020-12-05 18:41

    You forgot the dot of class selector of result class.

    Live Demo

    $(".result").hover(
      function () {
        $(this).addClass("result_hover");
      },
      function () {
        $(this).removeClass("result_hover");
      }
    );
    

    You can use toggleClass on hover event

    Live Demo

     $(".result").hover(function () {
        $(this).toggleClass("result_hover");
     });
    
    0 讨论(0)
提交回复
热议问题