2011-08-11 48 views
0

我設法得到了一個簡單的動畫效果,所以當我用類「gallery-wrap」翻轉我的div時,會出現帶有「magnifier」類的圖片在上面。jQuery - 無法獲得'this'的效果

我的問題是我有很多div類的「畫廊包裝」和大量的圖像類「放大鏡」。

$("img.magnifier").hide(); //this hides the image on page load 

    $('.gallery-wrap').hover(function() { 
     $("img.magnifier").show();    
      }, function() { 
     $("img.magnifier").hide(); 
    }); 

img.magnifier位於.gallery-wrap父DIV中,而不是內部.gallery-wrap

我需要它,所以這隻會做當前的徘徊元素,它已經在做什麼,但是它的動畫頁面上的所有img.magnifier

我認爲這將可能是這樣的......

$("img.magnifier").hide(); //this hides the image on page load 

    $('.gallery-wrap').hover(function() { 
     $(this).parent("img.magnifier").show();    
      }, function() { 
     $(this).parent("img.magnifier").hide(); 
    }); 

但不能讓它開始工作。

任何想法將是一個巨大的幫助謝謝。

+0

發佈您的實際html。父母(「img.magnifier」)只有在父母是img.magnifier的情況下才有效。嘗試.parents(「img.magnifier」) – Jules

回答

1

你是親近:

$(this).parent("img.magnifier").show(); 

將其更改爲:

$(this).parent().find("img.magnifier").show(); 

那麼它應該工作。當然你也可以用hide()做同樣的事情。

+0

這看起來好戲。感謝您的幫助! – Joshc

+0

@user不客氣:)很高興我能幫到你 – Paulpro

2

它不應該是這樣的:

$('.gallery-wrap').hover(function() { 
    $(this).find("img.magnifier").show();    
     }, function() { 
    $(this).find("img.magnifier").hide(); 
}); 

如果我理解正確,img.magnifier.gallery-wrap一個孩子,所以find()應該用來代替parent()

+0

你好,對不起,我嚴重地解釋了這個問題。我的意思是img.magnifier位於.gallery-wrap的父級div中 - 所以這個單詞對我來說.. $(this).parent()。find(「img.magnifier」)。顯示(); – Joshc

-1

你需要改變:

$(this).parent("img.magnifier") 

到:

$(this).find("img.magnifier") 

因爲img標籤是您的畫廊外包裝的子元素。

+0

我認爲這是downvoted,因爲我建議使用查找或兒童功能,當查找是適當的選擇這種情況? – RoccoC5

+0

沒有img位於gallery-wrap parent div中。 – Joshc

0

你能分配匹配的id值到gallery-wrap和關聯的img.magnifier嗎?

$('.gallery-wrap').hover(function() { 

    var id = $(this).attr('id'); 
    $("img.magnifier#"+id).show();    
     }, function() { 
    $("img.magnifier#"+id).hide(); 
}); 
-1

由於img.magnifier不在.gallery-wrap中,find()不會在這裏執行。

試試這個:



    $("img.magnifier").hide(); //this hides the image on page load 

    $('.gallery-wrap').hover(function() { 
     $(this).parents("img.magnifier").last().show();    
      }, function() { 
     $(this).parents("img.magnifier").last().hide(); 
    }); 

的。去年()是必要的,如果你有比父母只有一個img.magnifier()集合。