views:

419

answers:

3

Hi Guys, first post...

I have several pages to do with several table on each page. I've got the JQUERY to do zebra stripes on even lines and change colour on hover. but, here it comes, it changes colour on THEAD and TFOOT and i can't apply css to TFOOT to change colour.

Thanks very much in advance.... here's the code

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;

<script type="text/javascript">
$(document).ready(function() {
    $(function() {
        $('tr').hover(function() {
            $(this).css('background-color', '#FFFF99');
        },
        function() {
            $(this).css('background-color', '');
        });
    });
});

$(function() {
    $("table.tiger-stripe tr:odd").addClass("oddrow");
});
</script>
+2  A: 

apply the striping to table tbody only

$(function() { 
    $('table.tiger-stripe tbody tr').hover(
        function() { $(this).css('background-color','#FFFF99'); }, 
        function() { $(this).css('background-color', '#000000');}

    );       
    $("table.tiger-stripe tbody tr:odd").addClass("oddrow");  
 });
Russ Cam
thanks for the quick reply, I'm new to using jquery, and have been copying and pasting elements of the code to get it this far, I've ordered a book to get to grips with it, any pointers on how to do the answer you gave?
I've tidied up your code and put it all inside one function that will execute when the DOM has loaded - this is the $(function { ... }); code that wraps the inner jQuery selectors
Russ Cam
Hi Russ, thanks again, it works, brilliantly, I had to take out the #000000 colour as it left it black afterwords leaving it at '' goes back to original stripe colour. I don't know if this is 'good scripting'. Thanks very much - that's 5 hours down just figuring that out...
is there anyway to get it so that on multiple tables it always starts with the same colour - ive read about .child but not sure where to put it????
+1 Russ. Couldn't have put it better myself! :-)
Ian Roke
If you want it on all tables with the same layout then just do $('tbody tr').hover(... and $('tbody tr:odd').addClass(...
Ian Roke
Thanks Russ - changed one line to $("table.tiger-stripe tbody tr:nth-child(even)").addClass("evenrow"); });thanks everyone whose helped!! Stu
I was just about to add that you want to use nth-child - I even wrote up a demo for you here http://jsbin.com/epaje . You can edit it by adding /edit to the URL
Russ Cam
I would highly recommend jQuery in Action as a good introduction to jQuery - http://manning.com/bibeault/ . You may also want to check out JavaScript the Definitive Guide too - http://oreilly.com/catalog/9780596101992/ . You can be very productive from the outset with jQuery, but there comes a point where understanding JavaScript is necessary.Some might even argue that it's a prerequisite to using jQuery but I disagree - you can be productive whilst abstracted from the code so long as you understand that one day you will need to look into it in order to debug, write more efficient code, etc..
Russ Cam
A: 

Please can we see your code for a table? Are you sure there is more than one tr row in the tfoot because :odd will only trigger on alternate rows so a minimum of two rows must be present.

Oh did you mean that you didn't want tfoot to trigger?

In that case just use $('tbody tr:odd') as your selector.

Ian Roke
hi, thanks for the reply tried it, it now lets me style thead and tfoot but still highlights then when i roll my mouse over...any ideas???
It will pick up all the odd tr rows in tbody. Please be more specific about when it highlights and when you would like it to highlight. Does the code Russ posted work?
Ian Roke
A: 

Thanks everyone....

here's what the code looks like to do it ....

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"&gt;
</script><script type="text/javascript">
$(function() {
        $('table.tiger-stripe tbody tr').hover(
       function() { $(this).css('background-    color','#FFFF99'); },
       function() { $(this).css('background-    color', '');}    );
        $("table.tiger-stripe tbody tr:nth-child(even)").addClass    ("oddrow");   });

</script>
Thanks for that but there is no data in tbody and it is below tfoot which is not normally the right way to do it. Try the code in my answer I gave above.
Ian Roke
I see what has happened to your post. When you post code you need to make sure each line is indented four spaces so it can work out that it is a code block.
Ian Roke
Whenever you need to add code or new information to your question, please simply click "edit" below your question to edit in the new details. Please only use answers for actually answering a question (even your own). :)
Paolo Bergantino
@stu, I'd recommend deleting this "answer" and editing your original question to add this additional detail
Russ Cam