[英]Selecting image using $('img[src$=“image source end string in variable”]') getting undefined value
I have this code wherein there's an image in id #imgfullview
and images on class imglist
. 我有这段代码,其中在ID
#imgfullview
有一个图像,在类imglist
上有一个图像。
I want to get the image src of #imgfullview
and get the matching image on class imglist base on src attribute. 我想获取
#imgfullview
的图像src并基于src属性在类imglist上获取匹配的图像。
I used this selector https://api.jquery.com/attribute-ends-with-selector 我使用了这个选择器https://api.jquery.com/attribute-ends-with-selector
This is working if i just used selector using string 如果我只是使用选择器使用字符串,这是工作
var x = $(".imglist[src$='b-640x624.jpg']");
var imglistmatched = x.attr("src");
alert(imglistmatched );
But if I used a selector changing the string to variable, I'm getting undefined value
not sure why though I have exact string as 'b-640x624.jpg'
on variable splitimgurl
from string split function based on full patch src or through assigning the exact string if I check it on console.log or alert the variable 但是,如果我用了一个选择改变字符串变量,我得到
undefined value
不知道为什么,虽然我有确切的字符串作为'b-640x624.jpg'
可变splitimgurl
基于全补丁SRC或者通过从字符串分割功能分配确切的字符串(如果我在console.log上检查它或警告变量
var currentimg =$("#imgfullview").attr("src");
var splitimgpath = currentimg.split('_');
var splitimgurl = splitimgpath[2]; //or this splitimgurl = "b-640x624.jpg";
var x = $(".imglist[src$=splitimgurl]");
var imglistmatched = x.attr("src");
alert (imglistmatched);
I tried this 我试过了
var x = $(".imglist[src$="splitimgurl"]");
and this 和这个
var x = $(".imglist[src$="+splitimgurl+"]");
and this 和这个
var x = $(".imglist[src$=""+splitimgurl]");
it does not work 这是行不通的
Please see this on fiddle http://jsfiddle.net/w7n3sh2v/ 请在小提琴上查看此http://jsfiddle.net/w7n3sh2v/
I found the related question below but it does not help 我在下面找到了相关问题,但无济于事
jQuery $("img[src=the_image_souce]").attr('src','new_src'); jQuery $(“ img [src = the_image_souce]”)。attr('src','new_src'); does not work
不起作用
May I know if there's a correct way to put variable on Attribute Equals Selector? 我是否可以知道是否有正确的方法将变量放在“属性等于选择器”上?
You missed ''
in the 您错过了
''
var x = $(".imglist[src$=" + splitimgurl + "]");
Try this 尝试这个
var x = $(".imglist[src$='" + splitimgurl + "']");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.