用js打印一个table标签

我正在尝试打印一个表格,但它会捕获整个页面,并且在移动模式下,我只想打印地址表类,如下所示:


HTML


<table class="addr-table">


 ... table content...


</table>

<a href="#" class="js-print-link">Print</a>

JS


<script>


    $('.js-print-link').on('click', function() {

      var printBlock = $(this).parents('.addr-table').siblings('.addr-table');

      printBlock.hide();

      window.print();

      printBlock.show();

    });      

</script>

我尝试了很多格式,包括关于 SO 的建议,这段代码表现最好,现在是最后一集。我怎样才能在普通 pc 模式而不是移动模式下只捕获表格


大话西游666
浏览 212回答 1
1回答

子衿沉夜

请试一试......它工作正常:<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset='utf-8'>&nbsp; &nbsp; <meta http-equiv='X-UA-Compatible' content='IE=edge'>&nbsp; &nbsp; <title>Page Title</title>&nbsp; &nbsp; <meta name='viewport' content='width=device-width, initial-scale=1'>&nbsp; &nbsp; <style>&nbsp; &nbsp; @media print {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*{visibility:hidden}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.printable,.printable *{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visibility:visible&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; this content is not printable&nbsp; &nbsp; </div>&nbsp; &nbsp; <table class="printable">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ... table content...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table>&nbsp; &nbsp; <a href="#" class="js-print-link" onclick="print()">Print</a></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript