I have this:
HTML:
<p class="Link"><a href="...">Test1</a></p>
<p class="Link"><a href="...">Test2</a></p>
<p class="Link"><a href="...">Test3</a></p>
jQuery
$(document).ready(function() {
$('.Link').hover(function() {
$('.Link a').css('color', 'black');
}, function() {
$('.Link a').css('color', 'white');
});
});
First of all, I need to change the anchor color when the mouse hovers over the paragraph, not just the anchor. Secondly, I need to do this without creating a unique id for each paragraph or anchor. With the code as it is, it changes the color for all three anchors tags. I only want it to change the color on the anchor contained within the paragraph I am currently hovering over. Is this possible?
Thanks!
You need to use
thiswhich refers to the specific element that received the event.Doing:
is effectively the same as doing:
Of course, you could always accomplish this using purely CSS.
EDIT:
If all you’re doing is changing some CSS attributes, you don’t really need javascript.
To use a purely CSS approach, do this:
Because you’re doing this on an
<a>element, it is supported on IE6. Starting with IE7 (and most other browsers) you can use the same technique on other elements too.