I have at least two "news boxes" in my website, using code like:
<a class="newsbox" href="#"><span>TITLE</span><div>read more</div></a>
<a class="newsbox" href="#"><span>TITLE</span><div>read more</div></a>
With this code, I hide the "read more" in this anchor tag:
jQuery("a.newsbox div").hide();
which works fine.
Now I want to make the "read more" visible only on the currently hovered news box.
I tried using this code:
jQuery("a.newsbox").hover(function() {
jQuery(this).hover(function() {
jQuery("div").fadeIn();
});
});
but that is for all anchor tags with the class "newsbox". How can I select the current element and just show the "read more" for the current hovered element?
Try something like this:
$('a.newsbox').hover( function(){
$(this).find('div').fadeToggle();
});
By using fadeToggle
, "read more" will also hide itself on mouseout.
jQuery("a.newsbox").hover(function() {
jQuery(this).find('div').fadeIn();
});
jQuery('div')
is selecting all div elements on the page. You need to give it context...
jQuery("a.newsbox").hover(function() {
jQuery("div", this).fadeIn();
});
Try this code:
jQuery('a.newsbox').hover(function(){
jQuery(this).children('div').fadeIn();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.