【发布时间】:2012-08-22 12:26:14
【问题描述】:
我有一个数据表,每行都有一个复选框,选中时表示用户希望该行包含在打印的表中。检查行时,jQuery将打印类添加到:
$("td .checkbox").click(function(e){
if (this.checked) {
$("#tr-"+this.id).addClass("print");
} else {
$("#tr-"+this.id).removeClass("print");
}
});
编辑:添加 HTML 以显示下面答案的 id 和类...
<tr id="tr-print-0" class="search-line-item-0 print">
<td>data</td>
<td>data...</td>
<td><input type="checkbox" id="print-0" name="print-0"></td>
</tr>
<tr id="tr-print-1" class="search-line-item-1">...
<tr id="tr-print-2" class="search-line-item-0">...
然后在我的 CSS 文件中我有 @media print{} 它将行的显示设置为无或块。
tr.search-line-item-0, tr.search-line-item-1 {
display: none;
}
tr.search-line-item-0.print, tr.search-line-item-1.print {
display: block;
}
问题是当您打印页面时只显示带有 display:block 的行,这是预期的行为,但看起来每一行都在第一列内呈现(第一个 <th> 跨越 @ 987654325@,然后所有其他 <th>s 在它们下面什么都没有)。到目前为止只在 Chrome 和 Firefox 中测试过,这两种浏览器的问题都是一样的。
目标是在不打开其他窗口或使用 iframe 的情况下快速检查行并打印它们。因此,如果有人知道也可以使用的替代解决方案:)
【问题讨论】:
-
请发布一个完整的示例(带有 CSS 的 HTML 文档)来演示该问题。你现在是例如引用
th元素,但代码 sn-ps 不包含此类元素。注意你不应该在tr上设置display: block,因为在正常情况下tr应该有display: table-row。 -
@JukkaK.Korpela,感谢 display: table-row 成功了。如果您想发布,我会将其标记为正确答案。
标签: jquery css printing html-table media