小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

前端打印功能實(shí)現(xiàn)(原生實(shí)現(xiàn),無需導(dǎo)任何包)

 hncdman 2023-02-14 發(fā)布于湖南

y∩__∩y 歸來

于 2022-06-10 15:16:43 發(fā)布

1366

 收藏 7

分類專欄: JavaScript 文章標(biāo)簽: 前端 javascript typescript

版權(quán)

JavaScript

專欄收錄該內(nèi)容

6 篇文章0 訂閱

訂閱專欄

打印功能實(shí)現(xiàn)

效果預(yù)覽

  

目錄

打印功能實(shí)現(xiàn)

效果預(yù)覽

一、html 寫法

二、js或ts寫法

總結(jié)

一、html 寫法

<div id="exportableTable" style="display: none;">

      <table border="1" style="border-collapse:collapse;width: 100%;text-align: center;">

        <tr rowspan="2">

          <!-- 表頭 -->

          <th colspan="7">經(jīng)費(fèi)賬(科目:)</th>

        </tr>

        <tr cospan="7">

          <!-- 表頭 -->

          <th rowspan="2">日期</th>

          <th rowspan="1">憑證</th>

          <th rowspan="2">摘要</th>

          <th rowspan="2">收方</th>

          <th rowspan="2">付方</th>

          <th rowspan="2">收或付</th>

          <th rowspan="2">余額</th>

        </tr>

        <tr cospan="7">

          <!-- 表頭 -->

          <th rowspan="1">順序號</th>

        </tr>

        <!-- 行 -->

        <tr>

          <td>1</td>

          <td>2</td>

          <td>3</td>

          <td>4</td>

          <td>5</td>

          <td>6</td>

          <td>7</td>

        </tr>

      </table>

    </div>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

二、js或ts寫法

代碼如下(示例):

    printFee () {

      const printContent = document.getElementById("exportableTable");

      // window.open() 彈出窗口

      const WindowPrt = window.open('', '', 'left=500px,top=500px,width=900,height=900,toolbar=0,scrollbars=0,status=0');//彈出一個空的窗口(設(shè)置好寬高)

      // write()方法用于寫入文檔內(nèi)容,可以傳多個參數(shù),寫入的字符串會按HTML解析

      WindowPrt.document.write(printContent.innerHTML);

      // 運(yùn)行document.write(),運(yùn)行完后,最好手動關(guān)閉文檔寫入document.close()

      WindowPrt.document.close();

      //方法將焦點(diǎn)設(shè)置到當(dāng)前窗口,也就是將窗口顯示在前(靠近屏幕)

      WindowPrt.focus();

      // 調(diào)用 print() 方法會產(chǎn)生一個打印預(yù)覽彈框,讓用戶可以設(shè)置打印請求。

      WindowPrt.print();

      // window.close()關(guān)閉瀏覽器窗口

      WindowPrt.close();

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

總結(jié)

關(guān)鍵在于html要使用 id='exportableTable’ , js通過document.getElementById(“exportableTable”)將要打印的內(nèi)容獲取,

通過.write()寫入文檔內(nèi)容到新彈出的窗口,

再調(diào)用 print() 方法產(chǎn)生一個打印預(yù)覽彈框,即可實(shí)現(xiàn)打印

————————————————

版權(quán)聲明:本文為CSDN博主「y∩__∩y   歸來」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/qq_43730595/article/details/125221010

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多