简体   繁体   中英

Change color of hover element?

I want to change the background color on an element when I hover on it, but i won't work!?

$(document).ready(function(){
$(".walkingRoute-container").hover(function(){
    $("walkingRoute-container").css("background","#02baff");
  });
});

Preciate som help. Thanks!

You forgot the dot before the class name in the selector.

Use $(this) to target the element that is actually hovered, otherwise you will change the background of all elements with that class. You probably also want a function that removes the background when you leave the element:

$(document).ready(function(){
  $(".walkingRoute-container").hover(function(){
    $(this).css("background","#02baff");
  },function(){
    $(this).css("background","");
  });
});

Should there be a . before the class name inside the funcion?

$("walkingRoute-container").css("background","#02baff");

应该

$(".walkingRoute-container").css("background","#02baff");

I removed my answer about jQuery because mortenDelfi, Clyde Lobo, Guffa and 3nigma has already said everything. I only want to point out that maybe you may achieve what you need in pure CSS with no JavaScript using :hover selector like this:

.walkingRoute-container:hover { background-color: #02baff }

See DEMO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM