我有一点 JQuery 和 javascrip 来用较小缩略图的 src 更改大图像的 src 属性。缩略图的数量会有所不同,具体取决于上传的图像。
我在下面的代码在单击缩略图时有效,但是要为每个缩略图创建 onclick 事件,我必须遍历列表中的每张照片并写出“$('#thumb_n').click(function(). .." 对于每个缩略图。
请问有没有更简单的方法来做到这一点?
<script>
$(document).ready(function(){
$('#thumb_1').click(function(){
changeImgSrc($('#detailImage'), "/images/photos/1/small/draughtDesign_01.jpg");
});
$('#thumb_4').click(function(){
changeImgSrc($('#detailImage'), "/images/photos/1/small/draughtDesign_02.jpg");
});
$('#thumb_5').click(function(){
changeImgSrc($('#detailImage'), "/images/photos/1/small/draughtDesign_03.jpg");
});
});
function changeImgSrc(targetImg, sourceMSallImgURL){
var thbSource = sourceMSallImgURL;
var lrgSource = thbSource.replace('/small/', '/large/');
targetImg.attr("src", lrgSource);
}
</script>
<div class="galleryPhotos">
<span class="galleryPic shadow"><img src="/images/photos/1/large/draughtDesign_01.jpg" id="detailImage" width="515" height="380" alt="photo description 1" /></span>
<ul>
<li><a href="javascript: return false;" title="photo description 1"><img src="/images/photos/1/small/draughtDesign_01.jpg" width="160" height="115" id="thumb_1" alt="photo description 1" /></a></li>
<li><a href="javascript: return false;" title="photo description 2"><img src="/images/photos/1/small/draughtDesign_02.jpg" width="160" height="115" id="thumb_4" alt="photo description 2" /></a></li>
<li><a href="javascript: return false;" title="photo description 3"><img src="/images/photos/1/small/draughtDesign_03.jpg" width="160" height="115" id="thumb_5" alt="photo description 3" /></a></li>
</ul>
</div>