2013-02-21 29 views
0

我有一個懸停功能(請參閱下面的代碼),它給出了我已經應用到的類上的文本的下拉段落。這在指定的類上工作正常。在多個實例中應用jquery

$('.job_container').hover(function(){ 
    $(".job_subheading").animate({top: "30px }, 200);}, 
     function(){ 
      $(".job_subheading").animate({ 
     top: "0px" }, 200); 
     }); 

但是我想添加更多這些類的實例與不同的下拉段落。當我這樣做,然後懸停在一個,下降適用於所有,因爲他們有相同的類。

我應該將類更改爲ID並重新編寫我希望使用的每個實例的代碼? 在多個實例中應用代碼的最新方法是什麼?

感謝您的幫助, 加文

+0

.job_subheading是有利於兒童.job_container的元素? – starowere 2013-02-21 09:45:56

+0

顯示您的HTML。 – 2013-02-21 09:46:01

回答

0

您應該使用兒童選擇:

$('.job_container').hover(function(){ 
$(this).children(".job_subheading").animate({ 
top: "30px }, 200);}, 
         function(){ 
$(this).children(".job_subheading").animate({ 
top: "0px" }, 200); 

所以,你的HTML必須是這樣的:

<div class="job_container"><div class="job_subheading"></div>something else</div> 
<div class="job_container"><div class="job_subheading"></div>something else</div> 
<div class="job_container"><div class="job_subheading"></div>something else</div> 
+0

圖例!我欠你一品脫! – 2013-02-21 09:51:53

+0

不錯!你可以通過郵寄) – starowere 2013-02-21 09:53:12