I have a bunch of li elements that I want to alternate in color using coefficients and coefficients, and then select them based on the mouse. In order not to highlight, I need to keep track of what color was odd or even. To do this, when I apply the highlight color, I first set an arbitrary attribute for it. Are there any flaws in this? Is there a better way? Here is 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?
source share