如何在jquery中交换图像


How do i swap out images in jquery

我有一个网站,用户可以交换三个产品,每个产品都有图像,所以我不想每次用户点击按钮时都进行ajax调用,我只想在html中显示图像URL,不需要在需要时抓取。。例如,这是我的html

    <img src="<?php print $product[$selected_product]['product_image']; ?>" width="auto" height="199" alt="" />
</div>
<p style="display:none;" class="image_<?php print $product["standard"]['product_id']; ?>"><?php echo $product["standard"]['product_image']; ?></p>
<p style="display:none;" class="image_<?php print $product["professional"]['product_id']; ?>"><?php echo $product["professional"]['product_image']; ?></p>
<p style="display:none;" class="image_<?php print $product["premium"]['product_id']; ?>"><?php echo $product["premium"]['product_image']; ?></p>

正如你所看到的,显示的是所选的,但如果用户选择了其他图像之一,我需要更改图像标签的src。。。这是我的jquery

var image = $(this).parents(".item").find(".image_" + this_id).text();
image = $.trim(image);
console.log(image)
$(this).parents(".item").find(".item-image img").attr("src", image);

但问题是console.log正确地打印出了图像url,但有时当我点击图像时,它不会改变,我会得到这个错误

Image corrupt or truncated: http://posnation.com/shop_possystems/image/data/1B.png

一个更清晰的方法是渲染三个<img>标记,并设置它们的CSS,使它们位于彼此之上。然后使用jQuery显示/隐藏函数来显示相应的函数。