2011-05-03 122 views
1

我在我的網站至少有兩個「消息盒子」,使用如下代碼:jQuery的選擇當前元素

<a class="newsbox" href="#"><span>TITLE</span><div>read more</div></a> 
<a class="newsbox" href="#"><span>TITLE</span><div>read more</div></a> 

有了這個代碼,我躲在這個錨標記上的「更多」:

jQuery("a.newsbox div").hide(); 

它工作正常。

現在我想讓「閱讀更多」僅在當前懸停的新聞框中可見。

我嘗試使用此代碼:

jQuery("a.newsbox").hover(function() { 
    jQuery(this).hover(function() { 
     jQuery("div").fadeIn(); 
    }); 
}); 

但這是與類「NewsBox中」所有錨標籤。我怎樣才能選擇當前元素,並只顯示當前懸停元素的「read more」?

回答

6

嘗試是這樣的:

$('a.newsbox').hover(function(){ 
    $(this).find('div').fadeToggle(); 
}); 

使用fadeToggle, 「更多」 也將自身隱藏鼠標移開。

+0

工作過,謝謝! :) – Marek123 2011-05-03 14:08:30

+0

+1對於fadeToggle() – 2011-05-03 14:19:26

1
jQuery("a.newsbox").hover(function() { 
    jQuery(this).find('div').fadeIn(); 
}); 
+0

作品,謝謝! :) – Marek123 2011-05-03 14:08:23

1

jQuery('div')正在選擇頁面上的所有div元素。你需要給它的上下文...

jQuery("a.newsbox").hover(function() { 
    jQuery("div", this).fadeIn(); 
}); 

http://api.jquery.com/jQuery/

0

試試這個代碼:

jQuery('a.newsbox').hover(function(){ 

    jQuery(this).children('div').fadeIn(); 

});