我对JQuery(和JS)很陌生。我正在尝试做一个非常简单的事情,它将更改我的图像属性,并且由于某种原因它不起作用。在过去的一个小时里,我一直在努力工作,似乎无法找到无法正常工作的逻辑原因。我也尝试$("#thumbnail-"+id).attr(...)
过,但不起作用。我不知道为什么。所以我把事情分解成下面的代码,仍然无法正常工作。由于某些原因,该属性不会更改。
这是我的代码(为了使事情变得更简单,我对其进行了注释):
$(".innerImage").click(function (e) {
var imageName = ($(this).find('img').attr('src').split('/'))[2]; //returns something like 4.jpg
var id = (imageName.split('.'))[0]; //returns just 4 (from 4.jpg)
var thumb = "#thumbnail-"+id; //returns #thumbnail-4
var thumbnail = 'product/thumb/'+id+'.jpg'; //returns product/thumb/4.jpg
var full = 'product/full_size/'+id+'.jpg'; //returns product/full_size/4.jpg
$(thumb).attr('src', thumbnail); //This is the crazy part that doesn't work
alert(1); //Did a trace and it reached here.
});
有效的东西,但我不能使用它们:
因此,这里的怪异之处是以下内容起作用了:
$('img').attr('src', 'product/thumb/test.jpg');
$('#thumbnail-4').attr('src', 'product/thumb/test.jpg'); //this worked perfect. Notice how it's just that specific id, where I didn't concatenate the 4 using the method in my code. The latter is more general
但是上述(后一种)弄乱了我所有的图像,而不是我想要的特定图像,即#thumbnail-id(其中id = 1,...)。任何人都可以指出我做错了什么。我唯一能看到的是,它不允许我连接字符串并使用它们。
编辑:HTML
<div id='imageArea-4' class='imageArea'>
<div class='leftSide'>
"<div class='innerImage'>
<img src='product/thumb/4.jpg' />
</div>
<div class='innerImage'>
<img src='product/thumb/4-002.jpg' />
</div>
</div>
<div class='rightSide' id='rightSide-4'>
<img id='thumbnail-4' src='product/thumb/4.jpg' />
<button class='zoomImg intense' data-image='product/full_size/4.jpg' id='zoomImg-4'></button>
</div>
</div>
看起来正在发生以下情况:
当你选择第一个图像,4.jpg
时,id
被设置为4
。在这种情况下,您可以'#thumbnail-4'
正确定位。但是,当您单击第二个图像上,4-002.jpg
中,id
获取设置为4-002
。因此,该脚本希望更新#thumbnail-4-002
不存在的源。
我整理了这个小提琴以显示断开连接。http://jsfiddle.net/fd2cwm8y/
我的建议是提供thumbnail
一个唯一的ID,因此它不取决于id
您从单击的图像中检索到的值,就像我在这里抛出的一样:http : //jsfiddle.net/7qt8wzot/
我希望这有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句