Jquery查找特定的img attr(src)

标签 jquery image

刚刚安装了一个图像切换器,遇到了一个小问题:

我隐藏了三个图像,当将鼠标悬停在相关缩略图上时,我想将当前的类添加到主图像(以停止隐藏它)。

$("#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/

相关文章:

html - 具有纵横比的 SVG 路径背景图像自动缩放

java - JTable 中的 JComboBox 中的图像在选择后仅显示路径名

javascript - jquery在div内创建动态div

html - Bootstrap 垂直对齐图像

javascript - 如何在点击不同的 div 时切换 div?

javascript - 如何仅使用 javascript 在短时间内更改按钮文本?

python - 如何使用 OpenCv + Python 找到 blob 的轮廓

java - 在 Android 中删除旋转图像不显示删除正确路径

javascript - 跨域动态设置iframe高度

javascript - :not() in various guises isn't working