为什么这个 jQuery hide() 不适用于图像 src 属性?

发布于 2025-01-05 22:42:13 字数 508 浏览 2 评论 0原文

我最近一直在尝试学习 jQuery,在尝试创建悬停效果时,我似乎遇到了一些障碍。谁能告诉我为什么这不起作用?

$(document).ready(function(){   
 $('img.close').hover(function(){
                        $(this).attr('src','images/1.png');
                    },function(){
                        $(this).attr('src','images/2.png');
                    });
});

编辑:我试图用另一个图像替换一个图像,同时将鼠标悬停在它上面,创建一个翻转效果,

我忘记提及该图像是使用 jQuery 创建并添加到其容器中的

编辑2:我设法找到了 img.class 的问题是使用动画回调函数创建的,似乎我无法对它做任何事情,除了在回调函数中代码是正确的但没有放置在正确的位置

I have been trying to learn jQuery lately and while tring to crate a hover effect I seemes to have hit a bit of a snag. Can anyone tell me why this isn't working?

$(document).ready(function(){   
 $('img.close').hover(function(){
                        $(this).attr('src','images/1.png');
                    },function(){
                        $(this).attr('src','images/2.png');
                    });
});

EDIT:I was trying to replace an Image with another Image while Hovering on it beasicly creating a rollover efect

I forgot to mention that the image is created and aded to its container with jQuery

EDIT2:I have managed to find the problem the img.class was created using a animate callback function and it seems that I can't do anything to it except in the callback function the code is corect but not placed in the right place

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2025-01-12 22:42:13

如果您的图像 src 将是 images/1.png,那么以下代码将创建翻转效果

$(document).ready(function(){   
 $('img.close').mouseover(function(){
                    $(this).attr('src','images/2.png');
                });
$('img.close').mouseleave(function(){
                    $(this).attr('src','images/1.png');
                });
});

编辑:

这是悬停

$(document).ready(function(e) {
    $('img.test').hover(function(e){
    $(this).attr('src','Img2.jpg'); //event In handler
    alert($(this).attr('src'))
},function(e){
    $(this).attr('src','Img.jpg'); //event Out handler
})
});

希望它可能有所帮助

If your image src will be images/1.png, then the following code will create rollover effect

$(document).ready(function(){   
 $('img.close').mouseover(function(){
                    $(this).attr('src','images/2.png');
                });
$('img.close').mouseleave(function(){
                    $(this).attr('src','images/1.png');
                });
});

EDIT:

This is the hover

$(document).ready(function(e) {
    $('img.test').hover(function(e){
    $(this).attr('src','Img2.jpg'); //event In handler
    alert($(this).attr('src'))
},function(e){
    $(this).attr('src','Img.jpg'); //event Out handler
})
});

Hope it may help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文