我正在尝试更改鼠标悬停时图像的网址,它工作正常,但我正在尝试的是,将鼠标悬停在div而不是图像上并更改图像网址
跟随jQuery
我:
$(".logo > img").on({
"mouseover" : function() {
this.src = 'images/logo-white.png';
},
"mouseout" : function() {
this.src='images/logo-black.png';
}
HTML
<div class="logo">
<img src="images/logo-white.png">
</div>
如何在div悬停上制作它?请帮忙!
答案 0 :(得分:4)
如果这是HTML
<div class="logo">
<img src="images/logo-white.png">
</div>
然后这将有效:
$(".logo").on({
"mouseover": function() {
$(this).find("img").attr("src", "images/logo-white.png");
},
"mouseout": function() {
$(this).find("img").attr("src", "images/logo-black.png");
}
});
替代方案:.hover - 使用较少的}
$(".logo").hover(
function() {$(this).find("img").attr("src", "images/logo-white.png");},
function() {$(this).find("img").attr("src", "images/logo-black.png");}
);
答案 1 :(得分:0)
$(".logo").on({
"mouseover" : function() {
$(".logo img").attr("src","images/logo-white.png");
},
"mouseout" : function() {
$(".logo img").attr("src","images/logo-white.png");
}
答案 2 :(得分:0)
试试这个
$('.logo').hover(function () {
$(this).find('img').attr('src','images/logo-white.png');
},
function () {
$(this).find('img').attr('src','images/logo-black.png');
}
);