简体   繁体   English

更改HTML表中特定列的背景颜色

[英]Change Background color for a specific column in a HTML table

I would like to change the background color of a specif column in a HTML table. 我想更改HTML表格中指定列的背景颜色。 Is it possible to use a property to do it. 是否可以使用属性来做到这一点。 I found the way to do it with rows. 我找到了处理行的方法。

tr.even {
    background-color: white;
}

If you are looking to style the background of a column, and not a row, you can try something like this (below). 如果您希望为列而不是行的背景设置样式,则可以尝试如下所示(如下)。 You'll need to change the selectors to match your HTML. 您需要更改选择器以匹配您的HTML。

td:first-child { background-color: #ccc; }

Below is a link to a fiddle, along with a way to add classes via JS. 以下是小提琴的链接,以及通过JS添加类的方法。 There are many ways, but since you are dealing with columns, remember that the CSS is being applied to the same td in each row you want to style. 有很多方法,但是由于要处理列,因此请记住,CSS应用于要设置样式的每一行中的相同td

JSFiddle 的jsfiddle

For modern browsers, you could use the nth-child() selector: 对于现代浏览器,可以使用nth-child()选择器:

element:nth-child(1) {
    background-color:...
}

Or if you wanted to use jQuery, use eq() : 或者,如果您想使用jQuery,请使用eq()

$(element).eq(0).css("background-color","...");

Note: the CSS nth-child selector is 1-based while the jQuery eq selector is 0-based. 注意:CSS nth-child选择器基于1,而jQuery eq选择器基于0。

$('table tr:even').css('background-color', '#e5e5e5');

You can use :even and :odd selector: 您可以使用:even:odd选择器:

$( "table tr:even" ).css( "background-color", "white" );
$( "table tr:odd" ).css( "background-color", "red" );

If you want to target the column then use: 如果要定位列,请使用:

$('table tr > :nth-child(even)').css('background-color','white');
$('table tr > :nth-child(odd)').css('background-color','red');

Why don't you try using the colgroup tag and combining that with the span attribute. 您为什么不尝试使用colgroup标记并将其与span属性结合使用。 that way if you have 3 columns for instance, you can do 这样,如果您有3列,则可以

<colgroup>
<col span="2" id="something">
<col id="something2">
</colgroup>

then you can style that whole column with css. 那么您可以使用CSS为整个列设置样式。

ex: 例如:

#something2 {background-color:red;}

If you want to use just HTML and css you have to give every div even and odd class respectively. 如果只想使用HTML和CSS,则必须分别给每个div偶数和奇数类。 then give different css in you css file for both even and odd class. 然后在css文件中为偶数和奇数类提供不同的css。

<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>


    tr.even {
        background-color: white;
    }

    tr.odd {
        background-color: black;
    }

if you want more dynamic way ,mean don't give class manually jquery will serach and add even odd div and give them specific class 如果您想要更多动态的方法,则意味着不要手动给类,jQuery会搜索并添加偶数div并给他们特定的类

Use jquery 使用jQuery

$('tr:odd').addClass("odd")
$('tr:even').addClass("even")

and your css will be like this 而您的CSS会像这样

tr.even {
    background-color: white;
}

tr.odd {
    background-color: black;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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