我正在尝试查找具有与变量匹配的“src”的图像。我已使用“alert”来确保变量“currentThumbnail”正确,因此我认为问题在于语法。这就是我现在所拥有的:
var currentImage = $('.imageMain').attr('src');
var currentThumbnail = currentImage.substring(6);
var nextT = $('.thumbnails a img[src=currentThumbnail]');
问题出在“img[src=currentThumbnail]”。我需要更改什么才能使其有效?
如果不清楚,我很乐意添加更多信息。谢谢!
最佳答案
var nextT = $('.thumbnails a img[src=currentThumbnail]');
应该是
var nextT = $('.thumbnails a img[src="'+ currentThumbnail +'"]');
您需要字符串连接,因为您在字符串中使用变量 currentThumbnail
。
@ThiefMaster 的重要评论
The attribute values in selectors should be quoted. The docs says it's a requirement and I think the only reason why it's not enforced is backwards compatibility
关于jquery - 如何在 jQuery 选择器中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10962115/