如何让图像链接的背景颜色淡化为另一种颜色?

时间:2011-01-02 20:07:49

标签: javascript jquery

我实际上能够在我的博客上实现这一点:roseannebarr.tumblr.com 但它只适用于第一个...任何想法为什么?

        $(document).ready(function(){
            $("#block").hover(function() {
            $(this).stop().animate({ backgroundColor: "#a7bf51"}, 800);
            },function() {
            $(this).stop().animate({ backgroundColor: "#6cb4e2" }, 800);
            });

     }); 

1 个答案:

答案 0 :(得分:3)

您不能在同一页面上重复使用HTML id,它无效,您会看到完全这样的问题。相反,对于具有相同样式和/或行为的多个元素,请在元素上使用类似class="block"的类,并使用匹配的.class selector

 $(document).ready(function(){
   $(".block").hover(function() {
     $(this).stop().animate({ backgroundColor: "#a7bf51"}, 800);
   },function() {
     $(this).stop().animate({ backgroundColor: "#6cb4e2" }, 800);
   });
 });