简体   繁体   English

jQuery Each循环-显示单个项目

[英]jQuery Each Loop - display individual item

I have a loop of products in my view, which works great. 我认为我有一系列的产品,效果很好。

Then I coded the mouseover-popup (JS). 然后,我编码了mouseover-popup(JS)。 The popup displays a loop of all the products, rather than the individual product the user is viewing. 弹出窗口显示所有产品的循环,而不是用户正在查看的单个产品。

Could anyway teach me how to solve this? 无论如何可以教我如何解决这个问题?

Haml: 哈姆:

#product_view
- @products.each do |product|
    .product_each
        .product_image
            = link_to (image_tag product.image.url(:tiny)), product, class: "each", popup: true
        .pop-up
            %poptitle= product.title 

JS: JS:

$(function() {
    var moveLeft = -100;
    var moveDown = -150;

    $('a#link').hover(function(e) {
        $('.pop-up').show()
            .css('top', e.pageY + moveDown)
            .css('left', e.pageX + moveLeft)
            .appendTo('body');
    }, function() {
        $('.pop-up').hide();
    });

    $('a#link').mousemove(function(e) {
        $(".pop-up").css('top', e.pageY + moveDown).css('left', e.pageX + moveLeft);
    });

});

EDIT: 编辑:

class instead of id (named "each") class而不是id (命名为“ each”)

    $(function() {
  var moveLeft = -100;
  var moveDown = -150;

  $('a.each').hover(function(e) {
    $('.pop-up').show()
      .css('top', e.pageY + moveDown)
      .css('left', e.pageX + moveLeft)
      .appendTo('body');
  }, function() {
    $('.pop-up').hide();
  });

  $('a.each').mousemove(function(e) {
    $(".pop-up").css('top', e.pageY + moveDown).css('left', e.pageX + moveLeft);
  });

});

Where you're using $('.pop-up') , you're getting a list of every element with the class pop-up on the page. 在使用$('.pop-up') ,您将获得每个元素的列表以及页面上的类pop-up You should use $(this).parent().find('.pop-up') to find child .pop-up elements of each link's parent. 您应该使用$(this).parent().find('.pop-up')查找每个链接的父级的子级.pop-up元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM