0

我有以下html:

<img class="img-polaroid img-hover" src="/images/147/09e65aac97caf00c5414593719d0e1b4-medium.jpg?1376027188" background-color="yellow">

和CSS

.img-hover:hover{
  background-color: $green-highlight-1;
  cursor: pointer;
}

并希望转换为 jquery,以便我们可以更好地控制颜色(并为其设置动画)。就像是:

$('.img-hover').on('mouseover', function(){
    console.log("here i am in img-hover");
   $(this).attr('background-color','yellow');
});

但这不起作用(console.log 有效)。如何以这种方式设置背景颜色?

4

3 回答 3

0

你要

$(this).css('background-color','yellow');
于 2013-08-21T21:49:35.247 回答
0

使用css代替attr

$('.img-hover').on('mouseover', function(){
    console.log("here i am in img-hover");
   $(this).css("background-color", "yellow");
});
于 2013-08-21T21:49:45.190 回答
0

使用.css()方法,例如:

$('.img-hover').mouseover(function(){
  $(this).css('backgroundColor', ' yellow');
});
于 2013-08-21T21:49:54.063 回答