gpt4 book ai didi

javascript - 如何只打印选定的 HTML 元素?

转载 作者:技术小花猫 更新时间:2023-10-29 11:45:18 27 4
gpt4 key购买 nike

我正在尝试在 HTML 中实现打印功能。我知道我可以用 window.print() 打印整页,但如何只打印特定的页面元素?例如一个特定的 <DIV>Some text to print</DIV> .

最佳答案

您可以使用特定于打印的 CSS 样式表并隐藏除您想要打印的内容之外的所有内容。

<div class="no-print">I won't print</div><div class="something-else">I will!</div>

只是 no-print类将被隐藏,但任何带有打印类的内容都会显示。

<style type="text/css" media="print">
.no-print { display: none; }
</style>

关于javascript - 如何只打印选定的 HTML 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500962/

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