我如何在jquery中换出图像

时间:2011-06-06 19:43:38

标签: php javascript jquery

我的网站用户可以换出三种产品,每种产品都有图像,而不是每次用户点击按钮时都进行ajax调用,我想在html中显示图片网址并显示无需在需要时抓取..例如,这是我的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正确打印出图像网址,但有时当我点击图片时没有改变,我收到此错误

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

1 个答案:

答案 0 :(得分:1)

更明确的方法是渲染三个<img>标记并设置它们的CSS,使它们彼此重叠。然后使用jQuery show / hide函数显示相应的函数。