这是我的代码:
<html> <style> .left-info { font-size:14px; font-family:Tahoma,Helvetica,sans-serif; color:#1A5B71; font-weight:bold; text-align:right; } .right-info { font-size:14px; font-family:Tahoma,sans-serif; color:#FFFFFF; font-weight:bold; text-align:left; } </style> <body> <table border="1"> <colgroup> <col class="left-info" /> <col class="right-info" /> </colgroup> <tr> <td>3476896</td> <td>My first HTML</td> </tr> <tr> <td>5869207</td> <td>My first CSS</td> </tr> </table> </body> </html>
但是,它显示的是简单的表格.需要帮忙 !!
解决方法
看这里
http://www.w3.org/TR/CSS21/tables.html#columns
http://www.w3.org/TR/CSS21/tables.html#columns
您只能使用cols设置边框,背景,宽度和可见性
编辑
jQuery解决方案
使用这个小jQuery代码片段,您可以将col标记中的所有类名复制到相应的td标记
它甚至可以在coland td标签和嵌套表中使用colspan.
JavaScript的
$(document).ready(function() { var find_TDs_at_COL = function(table,col) { var ret = []; $(table).children('tbody').children('tr').each(function() { var col2 = 0; $(this).children('td,th').each(function() { oldCol2 = col2; if ($(this).attr('colspan')) { col2 += parseInt($(this).attr('colspan')); } else { col2++; } if (oldCol2 <= col && col2 > col) { ret.push(this); } }) }) return $(ret); } $('table > colgroup').each(function() { var $table = $(this).parent(); var col = 0; $(this).children('col').each(function() { var oldCol = col if ($(this).attr('colspan')) { col += parseInt($(this).attr('colspan')) } else { col++; } for (var i = oldCol; i < col; i++) { find_TDs_at_COL($table,i).addClass($(this).attr('class')) } }) }) })
$(document).ready(function() { "use strict"; var find_TDs_at_COL = function(table,th').each(function() { var oldCol2 = col2; if ($(this).attr('colspan')) { col2 += parseInt($(this).attr('colspan')); } else { col2++; } if (oldCol2 <= col && col2 > col) { ret.push(this); } }) }) return $(ret); } $('table > colgroup').each(function() { var $table = $(this).parent(); var col = 0; $(this).children('col').each(function() { var oldCol = col if ($(this).attr('colspan')) { col += parseInt($(this).attr('colspan')) } else { col++; } for (var i = oldCol; i < col; i++) { find_TDs_at_COL($table,i).addClass($(this).attr('class')) } }) }) })
.left-info { font-size:14px; font-family:Tahoma,sans-serif; color:#00FFFF; font-weight:bold; text-align:left; } .extra-info { font-size:14px; font-family:Tahoma,sans-serif; color:#ff0000; font-style: italic; text-align:right; } .additional-info { font-size:10px; font-family:Tahoma,sans-serif; color:#ffdd00; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border="1"> <colgroup> <col class="left-info" /> <col class="right-info" /> <col class="extra-info" colspan="3"/> <col class="additional-info"/> <col /> </colgroup> <tr> <th>A</th> <th>B</th> <th>C</th> <th>C</th> <th>C</th> <th>D</th> </tr> <tr> <td>3476896</td> <td>My first HTML</td> <td></td> <td>Extra</td> <td>Yes</td> <td>Add</td> </tr> <tr> <td>5869207</td> <td>My first CSS</td> <td>Ugh</td> <td colspan="2"></td> <td>Don't trust</td> </tr> <tr> <td>54379</td> <td>My first JS</td> <td colspan="2">Trust</td> </tr> </table>