问题描述
|
我有一个关于JavaScript的问题,有一个大图片和一个缩略图,我的JavaScript函数更改了从缩略图中获取大图片的链接,它可以正常工作,但我也有一个高幻灯片,可以用于大图片图片,单击大图片时,其实际大小以高幻灯片显示,但是我有一个小问题,当我更改大图片的链接时,它会自动出现此图片的两个链接,一个在大图片中另一个在缩略图中,因此单击后需要删除缩略图中的另一个链接,所以这是我现在的脚本:
$(document).ready(function() {
$(\'.image\').click(function(event) {
event.preventDefault();
var imagePath = $(this).attr(\"href\");
var newImg = new Image;
newImg.onload = function(){
$(\'#big_picture2\').hide();
$(\'#big_picture2\').attr(\'src\',imagePath);
$(\'.product_image_large\').attr(\'href\',imagePath);
$(\'#big_picture2\').fadeIn(\'slow\');
};
newImg.src = imagePath;
});
});
解决方法
未经测试,但认为这应该可行:
$(document).ready(function() {
$(\'.image\').click(function(event) {
event.preventDefault();
//add big picture link to active thumbnail
$(\'.image.active\').attr(\'href\',$(\'#big_picture2\').attr(\'src\')).removeClass(\'active\');
//set new active thumbnail
$(this).addClass(\'.active\');
var imagePath = $(this).attr(\"href\");
//remove this href
$(this).removeAttr(\'href\');
var newImg = new Image;
newImg.onload = function(){
$(\'#big_picture2\').hide();
$(\'#big_picture2\').attr(\'src\',imagePath);
$(\'.product_image_large\').attr(\'href\',imagePath);
$(\'#big_picture2\').fadeIn(\'slow\');
};
newImg.src = imagePath;
});
});