简体   繁体   中英

set background colour for table when the page loads

I am a newbie to Jquery so apoligise if this is a silly question

I have seen many ways how to change the background of a table colour but when a certain row is clicked or hovered, however i want to set the background colour of my table to white when the page is loaded.

ie code i have saw is like below

 $(function() {
        $('tr').hover(function() {
            $(this).css('background-color', '#FFFF99');
            $(this).contents('td').css({'border': '1px solid red', 'border-left': 'none', 'border-right': 'none'});
            $(this).contents('td:first').css('border-left', '1px solid red');
            $(this).contents('td:last').css('border-right', '1px solid red');
        },
        function() {
            $(this).css('background-color', '#FFFFFF');
            $(this).contents('td').css('border', 'none');
        });
    });

You really don't need jquery to be able to do this. This can all be done in plain CSS.

tr:hover
{
   background-color:#FFFF99;
}
tr:hover td
{
   styles here...
}
tr:hover td:first
{
   styles here...
}

etc...

As for setting the background-color on your table, this can be done in plain CSS too:

table.myTableClass
{
   background-color:#fff;
}

To set the background colour of a table try this:

$(function() {
    $("#myTable").css("background-color", "#FFF");

    // $("tr").hover(......

});

Where myTable is the id of your table. However, if this is to always to be done on load of the page, put it in CSS as that's what it's designed for.

Write it down this code and it work when page load.....

 $(document).ready(function () {
   $("#table_id").css("background-color", "#FFFF99");
 });

where table_id is id of table.....

Add this code,

        $('table').css('background-color', '#FFFFFF');
        $('table').contents('td').css('border', 'none');

jsfiddle sample

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM