溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

js 客戶端打印html 并且去掉頁眉、頁腳的實例

發(fā)布時間:2020-08-21 14:22:53 來源:腳本之家 閱讀:450 作者:風吹De麥浪 欄目:web開發(fā)

print() 方法用于打印當前窗口的內(nèi)容,支持部分或者整個網(wǎng)頁打印。

調(diào)用 print() 方法所引發(fā)的行為就像用戶單擊瀏覽器的打印按鈕。通常,這會產(chǎn)生一個對話框,讓用戶可以取消或定制打印請求。

win10下測試ie11、chrome、firefox、360、edge 都可以成功去掉頁眉頁腳;

<!DOCTYPE html>
<html>
<head>
 <title>打印</title>
 <meta charset="utf-8">
 <style>
  .printBox {
   width: 300px;
   height: 300px;
   border: 1px solid blue;
  }
 </style>
 <!-- 打印的樣式-->
 <style media="print">
  @page {
   size: auto;
   margin: 0mm;
  }
 </style>
</head>

<body>
<div class="printBox">
 this is content!!!<br>
 點擊按鈕打印
</div>
<button onclick='print_page()'>打印</button>
</body>

<script type="text/javascript">
 function print_page() {
  if (!!window.ActiveXObject || "ActiveXObject" in window) { //是否ie
   remove_ie_header_and_footer();
  }
  window.print();
 }

 function remove_ie_header_and_footer() {
  var hkey_path;
  hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
  try {
   var RegWsh = new ActiveXObject("WScript.Shell");
   RegWsh.RegWrite(hkey_path + "header", "");
   RegWsh.RegWrite(hkey_path + "footer", "");
  } catch (e) {
  }
 }
</script>
</html>

以上這篇js 客戶端打印html 并且去掉頁眉、頁腳的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI