初学者的问题在这里。我将制作一个Jquery函数,将其悬停在其上时用于更改图像。我使用id名称作为选择器。如何使其具有通用性,以便不必为带有翻转图像的每个标签都拥有该功能的副本?

$("#home img").hover(
        function(){
            blah
        },
        function(){
            blah
        }
 );

最佳答案

您可以在不使用js的情况下进行过渡(只要您不需要与IE6兼容)。

HTML:

<div class="imgHover">
    <img class="default" src="...">
    <img class="roll" src="...">
</div>


CSS:

.imgHover .roll {
    display: none;
}

.imgHover:hover .roll {
    display: block;
}

 .imgHover:hover .default {
    display: none;
}


如果您需要与IE6兼容,这应该可以工作(不过未经测试):

$('.imgHover').hover(
     function() {
         $('.default', $(this)).hide();
         $('.roll', $(this)).show();
     },
     function() {
         $('.default', $(this)).show();
         $('.roll', $(this)).hide();
     }
}

07-24 09:38
查看更多