gpt4 book ai didi

javascript - 打印 HTML 表格中选定的行

转载 作者:行者123 更新时间:2023-11-28 06:06:00 24 4
gpt4 key购买 nike

我有一个看起来像这样的表格 jsfiddle现在我需要实现仅打印选定行的功能。可以通过单击每行右侧的复选框来选择行。有人可以告诉我如何完成它吗?

我已经实现了完整的表格打印功能

var divToPrint=document.getElementById("pretazna");
newWin= window.open("");
newWin.document.write(divToPrint.outerHTML);
newWin.print();
newWin.close();

最佳答案

尝试此代码...它不会创建弹出窗口,而是使用打印样式表隐藏行 ( demo )

CSS

@media print {
#print, tfoot, tbody tr:not(.printme) {
display: none !important;
}
}

Javascript

function matches(el, selector) {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches
var matches = document.querySelectorAll(selector),
i = matches.length;
while (--i >= 0 && matches.item(i) !== el) {}
return i > -1;
}

function closest(el, selector) {
while (el && !matches(el, selector)) {
el = el.parentNode;
}
return matches(el, selector) ? el : null;
}

document.querySelector('table').addEventListener('change', function(event) {
var target = event.target;
closest(target, 'tr').classList[target.checked ? 'add' : 'remove']('printme');
})

document.querySelector("#print").addEventListener('click', function() {
window.print();
});

关于javascript - 打印 HTML 表格中选定的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com