为什么JQuery点击事件会改变页面上的所有图像?
我有一个产品页面,其中包含图像缩略图以及用于封面和封底更改缩略图的链接。问题是,当您单击这些链接来更改缩略图时,产品缩略图其余部分的所有图像也会发生变化。
如何将点击事件绑定到该产品,以便仅更改缩略图?
This is my HTML (this list is repeated bellow for other products with different images):
<div class="product-main-image">
// Here I have the thumb image
<a href="Images/cards/us-sip-connect-5-front-hi-res.jpg" class="product-main-image-hover">
<img src="Images/cards/us-sip-connect-5-front.jpg" class="product-image" />
</a>
// When I click front or back links it's changing all the thumbs for other product as well.
<ul class="thumbs">
<li><a href="Images/cards/front.jpg" rel="Images/cards/front-hi-res.jpg">Front</a></li>
<li><a href="Images/cards/back.jpg" rel="Images/cards/back-hi-res.jpg">Back</a></li>
</ul>
</div>
This is the jQuery:
$('.thumbs a').live('click', function(){
// Get the thumb image tag attributes
var image_thumb = $(this).attr("href");
var image_hi_res = $(this).attr("rel");
// switch the image by removing the node and re-writing in the neccessary HTML
$('.product-main-image-hover').remove();
$('.product-main-image').append('<a href="' + image_hi_res + '" class="product-main-image-hover"><img src="' + image_thumb + '" class="product-image" /></a>');
return false;
});
希望有人能引导我走向正确的方向。谢谢。
I have a product page that has image thumbnails and links bellow for front cover and back cover to change the thumbnails. The problem is that when you click on those links to change the thumbnails ALL the image for the rest of the product thumbnails change as well.
How can I bind the click event to that product so only that thumbnail is changed?
This is my HTML (this list is repeated bellow for other products with different images):
<div class="product-main-image">
// Here I have the thumb image
<a href="Images/cards/us-sip-connect-5-front-hi-res.jpg" class="product-main-image-hover">
<img src="Images/cards/us-sip-connect-5-front.jpg" class="product-image" />
</a>
// When I click front or back links it's changing all the thumbs for other product as well.
<ul class="thumbs">
<li><a href="Images/cards/front.jpg" rel="Images/cards/front-hi-res.jpg">Front</a></li>
<li><a href="Images/cards/back.jpg" rel="Images/cards/back-hi-res.jpg">Back</a></li>
</ul>
</div>
This is the jQuery:
$('.thumbs a').live('click', function(){
// Get the thumb image tag attributes
var image_thumb = $(this).attr("href");
var image_hi_res = $(this).attr("rel");
// switch the image by removing the node and re-writing in the neccessary HTML
$('.product-main-image-hover').remove();
$('.product-main-image').append('<a href="' + image_hi_res + '" class="product-main-image-hover"><img src="' + image_thumb + '" class="product-image" /></a>');
return false;
});
Hope someone can lead me in the right direction. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$('.product-main-image-hover')
将选择具有该类的所有元素。所以它的remove
方法将删除页面上的所有元素。您基本上需要在当前产品中找到它。您可以使用 jQueryclosest
方法查找产品图像容器,然后找到所需的元素并删除。append
会将元素追加到其末尾,因此在这种情况下,您必须使用prepend
因为您必须在同一位置替换图像。$('.product-main-image-hover')
will select all the elements which have that class. Soremove
method on it will remove all the elements on the page. You need to basically find it within the current product. You can use jQueryclosest
method to find the product images container and then find the required element and remove.append
will append the elements at the end of it so in this case you have to useprepend
since you have to replace the image at the same location.