jquery在悬停时更改背景图像

时间:2010-09-17 00:48:15

标签: jquery hover background-image

我有几个div我想生成css背景图片。他们会在悬停时改变。是的,我知道我可以在css中轻松完成此操作,但需要在jquery中完成。

代码:

$(document).ready(function() {
    $('.hoverBox').css('background', 'url(img/box' + id + '.jpg)')  
    $('.hoverBox').hover(function(){
    $(this).css('background', 'url(img/box' + this.id + '_back.jpg)')                      
    },
    function(){
    $(this).css('background', 'url(img/box' + this.id + '.jpg)')                       
    });
});

HTML就像这样:

<div class="hoverBox" id="benefits">
content</div>
<div class="hoverBox" id="installation">
content</div>
<div class="hoverBox" id="shoponline">
content</div>

这应该很容易,但我似乎无法使其发挥作用。

非常感谢帮助! TIA。

1 个答案:

答案 0 :(得分:0)

id是什么?

是否在其他地方定义了一些神秘变量,或者您认为从选择器中读取id是一种神奇的方式吗?

我猜你想用each()来获取id。