hover effect jQuery
Posted
by Ori Cohen
on Stack Overflow
See other posts from Stack Overflow
or by Ori Cohen
Published on 2009-07-14T00:04:41Z
Indexed on
2010/05/20
19:20 UTC
Read the original article
Hit count: 437
jQuery
|best-practices
I have a bunch of li elements that I want to alternate in color using odds and evens, and then highlight based on mouse hover. In order to un-highlight I need to keep track of what the color used to be, odd or even. To do this when I apply the highlight color, I first set an arbitrary attribute to it. Are there any downsides to doing it this way? Is there a better way? Here's the code:
<script type="text/javascript">
var init = function(event){
$("li:odd").css({'background-color' : '#eeeeee', 'font-weight' : 'bold'});
$("li:even").css('background-color', '#cccccc');
//initial colors setup
$("li").hover(
function () //hover over
{
var current = $(this);
current.attr('old-background', current.css('background-color'));
current.css('background-color', '#ffee99');
}
, function() //hover out
{
var current = $(this);
current.css('background-color', current.attr('old-background'));
})
}
$(document).ready(init);
</script>
So is there a better way to do this?
© Stack Overflow or respective owner