当鼠标悬停在图像上方时,我试图更改图像的 src 属性。这个 JQuery 代码有什么原因不能工作吗?
$('.icon1').mouseover(function(){
$(this).find('img').attr('src', 'recursos/botban/maq1.png');
});
应该工作正常吗?这只是一个随意的测试,看看出了什么问题,但它仍然坏了。
我也尝试过 $('.icon1').hover(function(){...
,它也不起作用。
我真正想要的是...
$('.icon1').mouseover(function(){
var alt = $(this).find('img').attr('src')+'.png';
$(this).find('img').attr('src', $(this).attr('id')+''.png);
}).mouseout(function(){
$(this).find('img').attr('src', alt);
});
HTML每个图像如下...
<a class='icon1'><img src='recursos/botban/veh1.png'></a>
$('.icon1').mouseover(function(){
$(this).find('img').attr('src', 'recursos/botban/maq1.png');
});
Should work right? It's just an arbitrary test to see what was wrong, but it's still broken.
I've also tried with $('.icon1').hover(function(){...
, and it also does not work.
What I really want is more like...
$('.icon1').mouseover(function(){
var alt = $(this).find('img').attr('src')+'.png';
$(this).find('img').attr('src', $(this).attr('id')+''.png);
}).mouseout(function(){
$(this).find('img').attr('src', alt);
});
The HTML for each image is as follows...
<a class='icon1'><img src='recursos/botban/veh1.png'></a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一些事情,我会使用
mouseenter
而不是鼠标悬停
(自mouseout
将在进入子级时触发),并确保它在document.ready
处理程序内运行,就像这样:我不确定您想要什么悬停图像,但一般方法是存储它原来的内容并使用,然后在
mouseleave
。或者,只需将hover
放在本身上,如下所示:
A few things, I'd use
mouseenter
instead ofmouseover
(sincemouseout
will fire when entering a child), and also make sure it's running inside adocument.ready
handler, like this:I'm not sure exactly what hover image you want, but the general approach is to store what it was originally and use than then restoring it on
mouseleave
. Or, just put thehover
on the<img>
itself, like this: