简体   繁体   English

使用$('img [src $ =“变量中的图像源结束字符串”]“)选择图像以获得未定义的值

[英]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.

相关问题 img 标签在输入后获取 src 的值,但未显示图像 - img tag is getting value of src after input but image is not getting displayed 使用jQuery更改img的src属性后图像未显示 - image not getting displayed after changing src attribute of img using jquery src是图像交换上的未定义变量 - src is undefined variable on image swap jQuery:如何使用img.attr(&#39;src&#39;,&#39;img.png&#39;)动画化(淡入)图像源变化? - jQuery: How to animate (fade) change of image source using img.attr('src', 'img.png')? 如果查询字符串具有特定的类或ID,则将查询字符串添加到Image Source(img src) - Add query string to Image Source(img src) if it has a particular class or ID 如何在图片标签的src属性内传递字符串 <img src=“”> 使用jQuery / JavaScript的? - How to pass string inside src attribute of image tag <img src=“”> using jquery/javascript? 使用jquery获取图像src时返回未定义 - while getting image src using jquery returns undefined 使用php获取图像 - Get an image in php using the <img src=“” tag 如何验证图像是否可见/存在于前端使用<img src>或<a href>标签</a> - How to verify Image is visible/exists on front end using <img src> or <a href> tag Ajax img请求成功结束,但无法设置图像src - Ajax img request end with success but cannot set image src
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM