color of the
  • change in on mouse over and click event using jquery
  • 后端 未结 3 1139
    轮回少年
    轮回少年 2020-12-10 23:45

    I want to change the color of the li as red on mouse over. And keep the same color in click event also. I have the following list,

    
      
    
            
    3条回答
    •  执笔经年
      2020-12-10 23:55

      // CSS: Create the highlight accessible with two classnames.
      
      .highlight, .highlight_stay{
          color:red;
      }
      

      Jquery

      $(function(){
           $('li').hover(function(){
                $(this).addClass('highlight');
            }, function(){
                $(this).removeClass('highlight');
            });
      
            $('li').click(function(){
                 $(this).addClass('highlight_stay');
            });
      });
      

      To remove the click color when a different li is clicked change the last function to this:

      $('li').click(function(){
           $(li).removeClass('highlight_stay');
           $(this).addClass('highlight_stay');
      });
      

    提交回复
    热议问题