刚刚安装了一个图像切换器,遇到了一个小问题:
我隐藏了三个图像,当将鼠标悬停在相关缩略图上时,我想将当前的类添加到主图像(以停止隐藏它)。
$("#thumbs img").mouseenter(function(){
//this gets the url of the thumb
var imgSrc = $(this).attr("src");
//this removes the last 7 letters
imgSrc = imgSrc.slice(0,-7);
//this adds .jpg to the end
imgSrc = imgSrc + '.jpg';
//This removes all images class of current
$('#mainImg img').removeClass("current");
//I want this to find the image with the src of imgSrc and add the current class
$('#mainImg').find(img.attr("src",imgSrc)).addClass("current");
最后一行是问题所在,它不起作用 - 有什么建议吗?
最佳答案
您需要迭代图像并找到具有正确来源的图像。您可以通过将最后一行更改为以下内容来完成此操作:
$('#mainImg img').each(function(i,ele){
if ($(this).attr("src") == imgSrc) { $(this).addClass("current"); }
}
关于Jquery查找特定的img attr(src),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5443985/