Jquery 单击缩略图img,查找alt值。。。然后将类添加到具有相同alt值的所有图像
我有主要的图片和缩略图。我想在单击缩略图图像时将“活动”类添加到主图像。。。唯一的共同点是alt标记 这就是我尝试过的Jquery 单击缩略图img,查找alt值。。。然后将类添加到具有相同alt值的所有图像,jquery,Jquery,我有主要的图片和缩略图。我想在单击缩略图图像时将“活动”类添加到主图像。。。唯一的共同点是alt标记 这就是我尝试过的 $('#thumbnails img').click(function () { var alt = $(this).attr("alt"); var tag = $("img").attr(alt); $("#mainImages").find(tag).addClass("active"); }); 您需要使用它来获取带有alt值的标记var ta
$('#thumbnails img').click(function () {
var alt = $(this).attr("alt");
var tag = $("img").attr(alt);
$("#mainImages").find(tag).addClass("active");
});
您需要使用它来获取带有alt值的标记
var tag=$(“img[alt='“+alt+'”])代码>
这就是我从帖子和评论中解释的
$('#thumbnails img').click(function () {
var alt = $(this).attr("alt");
$('#thumbnails img', '#mainImages img').filter(function() { return $(this).attr('alt') == alt;}).addClass('active');
});
美好的但是“alt”属性值不能包含几个字符,例如]
。它将破坏选择器语法。我们如何解决这个问题并使解决方案防弹?IDK,但OP没有提到任何关于alt标记值将是什么或者至少它看起来是什么样子的。我做了一些研究(),并找到了答案(见下一条评论)$('img','#mainImages').filter(函数(){return$(this.attr('alt')==alt;}.addClass('active')代码>-“var tag”行变得不必要了,所以我根据帖子和评论修改了脚本。。。我将重写答案这看起来不错,但在我的环境中仍然不起作用。我感谢你的帮助!
$('#thumbnails img').click(function () {
var alt = $(this).attr("alt");
$('#thumbnails img', '#mainImages img').filter(function() { return $(this).attr('alt') == alt;}).addClass('active');
});