js打印去除页眉页脚url地址
时间: 2023-08-05 11:02:43 浏览: 47
在 JavaScript 中,要打印当前页面并去除页眉和页脚的 URL 地址,可以使用 `window.print()` 方法和 `@media print` CSS 媒体查询。
下面是一个示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Print without Header and Footer URL</title>
<style>
@media print {
@page {
margin-top: 0;
margin-bottom: 0;
}
.no-print {
display: none;
}
}
</style>
</head>
<body>
<h1>Print without Header and Footer URL</h1>
<p>This is a sample paragraph for printing without header and footer URL.</p>
<button class="no-print" onclick="window.print()">Print</button>
</body>
</html>
```
在上面的示例代码中,我们使用 `@media print` CSS 媒体查询来指定在打印时应用的样式。我们在 `@page` 中设置了页眉和页脚的上边距和下边距为 `0`,这样就可以去除页眉和页脚的 URL 地址。
我们还使用了一个类名为 `no-print` 的 CSS 类来隐藏页面中的打印按钮,因为我们不希望在打印输出中包含这个按钮。
当用户点击页面中的打印按钮时,`onclick` 事件会触发 `window.print()` 方法,这将启动打印对话框,在对话框中用户可以选择打印机和其他打印选项。