原生js打印指定節點元素

很簡單(可粘貼至txt文檔后改后綴為html打開看效果):

<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>打印</title><meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><div><span style="color:red">這里不打印 !!!</span>
</div><div id="printDiv"><span style="color:green">打印這里!!</span>
</div>
<button onclick="print()">打印</button><script>function print(){// 設置要打印內容的 id: printDivconst WindowPrt = window.open('', '打印', 'width=' + (screen.availWidth - 10) + ',height=' + (screen.availHeight - 50) + ',left=0,top=0,location=0,menubar=0,toolbar=0,scrollbars=0,status=0'); // 裸全屏WindowPrt.document.head.innerHTML = window.document.head.innerHTML // 加入當前所有頭 - 以防樣式丟失WindowPrt.document.body.innerHTML = document.getElementById('printDiv').innerHTML; // 載入指定bodyWindowPrt.print() // 調用打印WindowPrt.close() // 自動等待print結束后執行}
</script></body>
</html>

將此方法放在主頁,傳入參數,實現公用(用原生onclick()事件調用):

<script>// 打印var rootPrint = function (elId) {// 設置要打印內容的 id: elIdconst WindowPrt = window.open('', '打印', 'width=' + (screen.availWidth - 10) + ',height=' + (screen.availHeight - 50) + ',left=0,top=0,location=0,menubar=0,toolbar=0,scrollbars=0,status=0'); // 裸全屏WindowPrt.document.head.innerHTML = window.document.head.innerHTML // 加入當前所有頭 - 以防樣式丟失WindowPrt.document.body.innerHTML = document.getElementById(elId).innerHTML; // 載入指定bodyWindowPrt.print() // 調用打印WindowPrt.close() // 自動等待print結束后執行}</script>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278541.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278541.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278541.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android社會化分享詳解

前言現如今app市場競爭激烈&#xff0c;做app不會放過任何推廣自己的app的渠道&#xff0c;如果app中沒有社會化分享功能&#xff0c;那真的是OUT了&#xff0c;我們先來看下一些app中的分享界面功能吧。現在主流的分享平臺&#xff0c;一般用的都是微信、QQ、微博&#xff0c;…

windows7黑屏修復_如何在Windows 10更新后修復黑屏

windows7黑屏修復RealVector/Shutterstock.comRealVector / Shutterstock.comSome Windows 10 PCs have been rebooting to a black screen after installing the June 2019 cumulative update from Windows Update. This seems scary at first, but luckily there’s a quick …

[sol]250OJ 1~10

下載 轉載于:https://www.cnblogs.com/yztblog/p/10208314.html

vue/cli4 創建vue項目選項詳解

多版本創建項目一、vue-cli2.x二、vue-cli3.x三、vue-cli4.x1.查看 vue 版本&#xff1a; 項目中,找到package.json文件夾 找"dependencies"中的vue &#xff1b; 若無項目&#xff0c;在cmd中輸入 where vue&#xff0c;cd到vue目錄下輸入 npm list vue &#xff0c…

java 商品評價計算算法

import java.io.Serializable; import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; import java.math.BigDecimal; import java.math.RoundingMode;/*** 商品評價算法* * project icomment* fileName ProductScore.java* Description* author light-z…

rainmeter使用教程_如何使用Rainmeter在桌面上顯示報價

rainmeter使用教程I’ve never really been a desktop gadgets and widgets type of person, but I often put an inspirational quote on my desktop wallpaper. Today we’ll show you how to do this using Rainmeter, no matter what wallpaper you switch to. 我從來沒有真…

Some code changes cannot be hot swapped into a running virtual machine

java運行中修改代碼不能改變立刻應用到本次運行中轉載于:https://www.cnblogs.com/Pusteblume/p/10211110.html

自定義v-drag指令(橫向拖拽滾動)

指令 Vue.directive(drag, {// 鉤子函數&#xff0c;被綁定元素插入父節點時調用 (父節點存在即可調用&#xff0c;不必存在于 document 中)。inserted: (el, binding, vnode, oldVnode) > {console.log(el, binding, vnode, oldVnode)let drag el; // 要拖拽的元素// let …

javascript獲取時間差

function GetDateDiff(startTime, endTime, diffType) {//將xxxx-xx-xx的時間格式&#xff0c;轉換為 xxxx/xx/xx的格式 startTime startTime.replace(/\-/g, "/");endTime endTime.replace(/\-/g, "/");//將計算間隔類性字符轉換為小寫diffType diffTy…

JMeter擴展JMeter插件獲取更多監聽器

為了獲取更多監聽器&#xff0c;方便的監控系統及應用&#xff0c;有必要安裝第三方插件 插件下載地址&#xff1a; https://jmeter-plugins.org/downloads/old/ http://pan.baidu.com/s/1gfC11yN 注&#xff1a;如果插件和軟件版本不兼容&#xff0c;可能在開啟Jmeter時會報錯…

如何阻止Chrome(或Edge)接管媒體密鑰

Google Chrome now has built-in support for media keys. Unfortunately, Chrome will take over your media keys and prevent them from controlling apps like Spotify when you’re watching YouTube, for example. Here’s how to make Chrome ignore your media keys. G…

js滾動條滾動到指定元素

let item document.getElementById("item"); // 指定的元素 let wrapper document.getElementById("wrapper"); // 其父元素 - 必須是產生滾動條的元素// 元素聚焦法定位 // item.focus(); // 可用 outline:none; 除去聚焦產生的框; 對于默認沒有聚焦的…

開源性能測試工具JMeter快速入門(一)

目錄一、JMeter簡介二、JMeter功能介紹三、JMeter腳本四、關于JMeter小提示一、JMeter簡介1.定義JMeter是Apache組織開發的基于Java的壓力測試工具。用于對軟件做壓力測試&#xff0c;它最初被設計用于Web應用測試&#xff0c;但后來擴展到其他測試領域。 1&#xff09;它可以用…

八重州8900如何解鎖_八重貴族怪胎之路

八重州8900如何解鎖Dealing with computers day in and day out can be a harrowing experience. In difficult times, or even when things are idle, finding some spirituality can help cope with the experience—Techies: I give you the Eightfold Noble Geek Path. 日…

mysql 5.7.18 winx64安裝配置方法

在mysql-5.7.18-winx64文件夾下新建my.ini文件[mysql] # 設置mysql客戶端默認字符集 default-character-setutf8 [mysqld] #設置3306端口 port 3306 # 設置mysql的安裝目錄 basedirD:\Program Files\mysql-5.7.18-winx64 # 設置mysql數據庫的數據的存放目錄 datadirD:\Prog…

js 實現拖拽滾動、滾輪縮放元素函數和案例

文章目錄一、拖拽滾動1、封裝函數2、示例&#xff1a;二、滾輪縮放1、封裝函數2、結合拖拽滾動示例一、拖拽滾動 1、封裝函數 /*** description 使用鼠標拖拽div&#xff0c;實現橫向、縱向滾動* param el 被拖拽滾動的元素&#xff08;產生滾動條的元素&#xff09;*/functio…

怎么解決input中readonly屬性的iOS一直存在光標問題

用css中的pointer-events:none轉載于:https://www.cnblogs.com/studyh5/p/8352061.html

贈與大學畢業生_出售,贈與或交易iPhone之前應該做什么

贈與大學畢業生A factory reset of your iPhone erases all of your content and settings, reverting it to a like-new state. However, there are a few extra steps you should take if you plan to get rid of your iPhone. iPhone的恢復出廠設置將刪除所有內容和設置&…

layui radio 根據獲取的到值選中

<input type"radio" name"lwkg" value"1" title"開" lay-filter"lwkg"> <input type"radio" name"lwkg" value"0" title"關" lay-filter"lwkg"> layui.use(…

設置Mac自動顯示和隱藏 Dock 欄的速度

Dock 顯示和隱藏&#xff0c;系統默認設置成了1秒 通過終端.APP修改顯示和隱藏的時間 &#xff08;單位&#xff1a;秒&#xff09; 默認的&#xff1a;defaults write com.apple.dock autohide-delay -int 1 優化的&#xff1a;defaults write com.apple.dock autohide-del…