为什么JQuery点击事件会改变页面上的所有图像?

发布于 2024-12-04 03:34:49 字数 1448 浏览 1 评论 0原文

我有一个产品页面,其中包含图像缩略图以及用于封面和封底更改缩略图的链接。问题是,当您单击这些链接来更改缩略图时,产品缩略图其余部分的所有图像也会发生变化。
如何将点击事件绑定到该产品,以便仅更改缩略图?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2024-12-11 03:34:49

$('.product-main-image-hover') 将选择具有该类的所有元素。所以它的 remove 方法将删除页面上的所有元素。您基本上需要在当前产品中找到它。您可以使用 jQuery closest 方法查找产品图像容器,然后找到所需的元素并删除。 append 会将元素追加到其末尾,因此在这种情况下,您必须使用 prepend 因为您必须在同一位置替换图像。

$('.thumbs a').live('click', function(){  
   // Get the thumb image tag attributes
    var image_thumb = $(this).attr("href");
    var image_hi_res = $(this).attr("rel");

    var productMainImage = $(this).closest('.product-main-image');

    // switch the image by removing the node and re-writing in the neccessary HTML
    productMainImage.find('.product-main-image-hover').remove();

    productMainImage.prepend('<a href="' + image_hi_res + '" class="product-main-image-hover"><img src="' + image_thumb + '" class="product-image" /></a>');

     return false;
  });

$('.product-main-image-hover') will select all the elements which have that class. So remove method on it will remove all the elements on the page. You need to basically find it within the current product. You can use jQuery closest 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 use prepend since you have to replace the image at the same location.

$('.thumbs a').live('click', function(){  
   // Get the thumb image tag attributes
    var image_thumb = $(this).attr("href");
    var image_hi_res = $(this).attr("rel");

    var productMainImage = $(this).closest('.product-main-image');

    // switch the image by removing the node and re-writing in the neccessary HTML
    productMainImage.find('.product-main-image-hover').remove();

    productMainImage.prepend('<a href="' + image_hi_res + '" class="product-main-image-hover"><img src="' + image_thumb + '" class="product-image" /></a>');

     return false;
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文