5

这是我的代码:

<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, Helvetica, 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>

但是,它显示的是简单的表格。需要帮忙 !!

4

3 回答 3

8

看这里
http://www.w3.org/TR/CSS21/tables.html#columns

您只能设置border, background,width和svisibilitycol


编辑

jQuery解决方案

使用这个小的 jQuery 片段,您可以将标签中的所有类名复制col到相应的td标签中。它甚至适用于和标签中
的 colspan以及嵌套表。coltd

这里的例子是 jsfiddle

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'))
            }

        })
    })
})​

console.clear()
$(document).ready(function() {
    "use strict";
    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() {
                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
            var that = this
            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'))
                  
                  // copy style as well
                  // .each(function() {
                  //  const [ ...style ] = that.style
                  //  for ( let r of style ) {
                  //    this.style[r] = that.style[r]
                  //  }
                  //})
            }

        })
    })
})
.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, Helvetica, sans-serif;
    color:#00FFFF;
    font-weight:bold;
    text-align:left;
}
.extra-info {
    font-size:14px;
    font-family:Tahoma, Helvetica, sans-serif;
    color:#ff0000;
    font-style: italic;
    text-align:right;
  
}
.additional-info {
    font-size:10px;
    font-family:Tahoma, Helvetica, 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>

VanillaJS 解决方案

{
  "use strict";
  
  document.addEventListener('DOMContentLoaded', e => {
    Array.from(document.querySelectorAll('table > colgroup')).forEach(cg => {
      const table = cg.parentElement
      let col = 0
      Array.from(cg.querySelectorAll(':scope > col')).forEach(c => {
        const oldCol = col
        if (c.getAttribute('colspan')) {
          col += +c.getAttribute('colspan')
        } else {
          col++
        }
        for (let i = oldCol; i < col; i++) {
          find_TDs_at_COL(table, i).forEach(el => {
            Array.from(c.classList).forEach(c => el.classList.add(c))
          })
        }
      })
    })
  })

  const find_TDs_at_COL = (table, col) => {
    let ret = [];
    Array.from(table.querySelectorAll(':scope > tbody > tr')).forEach(tr => {
      let col2 = 0
      Array.from(tr.querySelectorAll(':scope > td, :scope > th')).forEach(tc => {
        const oldCol2 = col2
        if (tc.getAttribute('colspan')) {
          col2 += +tc.getAttribute('colspan')
        } else {
          col2++
        }
        if (oldCol2 <= col && col2 > col) {
          ret.push(tc);
        }
      })
    })
    return ret
    
  }
  
}
.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, Helvetica, sans-serif;
    color:#00FFFF;
    font-weight:bold;
    text-align:left;
}
.extra-info {
    font-size:24px;
    font-family:Tahoma, Helvetica, sans-serif;
    color:#ff0000;
    font-style: italic;
    text-align:right;
  
}
.additional-info {
    font-size:10px;
    font-family:Tahoma, Helvetica, sans-serif;
    color:#ffdd00;
  
}

.shadow {
  text-shadow: 2px 2px 0 black
}
.info {
  text-decoration: overline;
}
<table border="1">
    <colgroup>
      <col class="left-info" />
      <col class="right-info shadow 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>

<br><hr><br>


<table border="1">
    <colgroup>
      <col class="right-info" />
      <col class="left-info" />
      <col class="additional-info"/>
      <col class="extra-info shadow" colspan="3"/>
      <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>

于 2012-03-08T19:40:53.307 回答
2

尽管此时给出的答案已经有一年了,但我想我只是指出您可以使用非常简单的 CSS 轻松做到这一点

与其尝试将类赋予其列中的每个 td,不如简单地像这样定位它们:

td:first-child{
    color: #1A5B71;
    text-align: right;
}

td:last-child{
    color: #FFFFFF;
    text-align: left;
}

使用 JavaScript 来完成这个任务完全是矫枉过正

于 2016-01-15T00:48:43.733 回答
0

我为此编写了一个小的 jQuery 脚本,它将类应用于s 表中th的每个td元素。colspan

在这里试试

JavaScript:

$(function () {
  $('colgroup').each(function () {
    var $colgroup = $(this)
    var classes = $colgroup.children().map(function () { return $(this).attr('class') })
    $colgroup.siblings().children('tr').each(function () {
      var col = 0
      $(this).children().each(function () {
        var $child = $(this)
        $child.addClass(classes[col])
        col += parseInt($child.attr('colspan')) || 1
      })
    })
    $colgroup.remove()
  })
})

脚本并不复杂,但步骤如下:

  1. 对于每一个colgroup
    • 缓存cols 拥有的类名
  2. 对于tr同一个表中的 每个
    • 将变量设置col为 0
  3. 对于tr( ths 和tds) 的每个孩子
    • 添加一个类,选择col
    • col按其属性递增,colspan如果不存在则递增 1,以便在下一次迭代时,脚本将知道要选择哪个类
  4. 完全删除colgroup,因为例如,您可能有一个不透明度为 1 的背景,这将导致您th的 s 和tds 的背景具有错误的不透明度。

我缓存$(this)了几次,因为缓存 jQuery 对象比$()每次要选择一个元素时调用更快。

于 2016-07-08T15:43:10.750 回答