CSS如何设置多列每隔交叉颜色

CSS如何设置多列每隔交叉颜色


慕码人8056858
浏览 493回答 3
3回答

心有法竹

CSS设置多列每隔交叉颜色的方法:在css里面写奇数和偶数行的判断逻辑,并给出不同的颜色值即可。代码如下:<html>&nbsp;<head>&nbsp;<title>Table隔行变色</title>&nbsp;<style>&nbsp;<!--&nbsp;&nbsp; &nbsp; tr{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; background: #f00; //设置背景色为red,红色&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; tr:nth-child(2n){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; background: #ccc;&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; tr{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; background-color: expression((this.sectionRowIndex % 2 == 0) ? &nbsp; "#f00" : "#ccc" ); //奇数行设置为f00,偶数行设置为ccc&nbsp; &nbsp; }&nbsp;-->&nbsp;</style>&nbsp;</head>&nbsp;<body>&nbsp;<table>&nbsp;&nbsp; &nbsp; <tr><td>111111111</td></tr>&nbsp;&nbsp; &nbsp; <tr><td>222222222</td></tr>&nbsp;&nbsp; &nbsp; <tr><td>333333333</td></tr>&nbsp;&nbsp; &nbsp; <tr><td>444444444</td></tr>&nbsp;</table>&nbsp;</body>&nbsp;</html>&nbsp;运行效果:&nbsp;

慕仙森

css3有个nth-child(odd)和nth-child(even);如:123456789101112131415161718192021222324<!DOCTYPE&nbsp;html><html><head><style>&nbsp;p:nth-child(odd){color:red;}p:nth-child(even){color:blue;}</style></head><body>&nbsp;<h1>这是标题</h1><p>第一个段落。</p><p>第二个段落。</p><p>第三个段落。</p><p>第四个段落。</p>&nbsp;</body></html>但是css3,IE浏览器9以下都不兼容,如果需要考虑IE的兼容性的话,建议使用jq设置,如:1234$(document).ready(function(){&nbsp;&nbsp;&nbsp;&nbsp;$("p:nth-child(even)").css("color","blue");&nbsp;&nbsp;&nbsp;&nbsp;$("p:nth-child(odd)").css("color","red");});&nbsp; &nbsp;&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

CSS3