这个代码工具类是师兄发给我的,我也不知道哪来的,自己修补了一下,收藏备用。

    1. // 打印类属性、方法定义
    2. const Print = function (dom, options) {
    3. if (!(this instanceof Print)) return new Print(dom, options);
    4. this.options = this.extend({
    5. 'noPrint': '.no-print'
    6. }, options);
    7. if ((typeof dom) === "string") {
    8. this.dom = document.querySelector(dom);
    9. } else {
    10. this.isDOM(dom)
    11. this.dom = this.isDOM(dom) ? dom : dom.$el;
    12. }
    13. this.init();
    14. };
    15. Print.prototype = {
    16. init: function () {
    17. var content = this.getStyle() + this.getHtml();
    18. this.writeIframe(content);
    19. },
    20. extend: function (obj, obj2) {
    21. for (var k in obj2) {
    22. obj[k] = obj2[k];
    23. }
    24. return obj;
    25. },
    26. getStyle: function () {
    27. var str = "",
    28. styles = document.querySelectorAll('style,link');
    29. for (var i = 0; i < styles.length; i++) {
    30. str += styles[i].outerHTML;
    31. }
    32. str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
    33. return str;
    34. },
    35. getHtml: function () {
    36. var inputs = document.querySelectorAll('input');
    37. var textareas = document.querySelectorAll('textarea');
    38. var selects = document.querySelectorAll('select');
    39. for (var k = 0; k < inputs.length; k++) {
    40. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
    41. if (inputs[k].checked == true) {
    42. inputs[k].setAttribute('checked', "checked")
    43. } else {
    44. inputs[k].removeAttribute('checked')
    45. }
    46. } else if (inputs[k].type == "text") {
    47. inputs[k].setAttribute('value', inputs[k].value)
    48. } else {
    49. inputs[k].setAttribute('value', inputs[k].value)
    50. }
    51. }
    52. for (var k2 = 0; k2 < textareas.length; k2++) {
    53. if (textareas[k2].type == 'textarea') {
    54. textareas[k2].innerHTML = textareas[k2].value
    55. }
    56. }
    57. for (var k3 = 0; k3 < selects.length; k3++) {
    58. if (selects[k3].type == 'select-one') {
    59. var child = selects[k3].children;
    60. for (var i in child) {
    61. if (child[i].tagName == 'OPTION') {
    62. if (child[i].selected == true) {
    63. child[i].setAttribute('selected', "selected")
    64. } else {
    65. child[i].removeAttribute('selected')
    66. }
    67. }
    68. }
    69. }
    70. }
    71. return this.dom.outerHTML;
    72. },
    73. writeIframe: function (content) {
    74. var w, doc, iframe = document.createElement('iframe'),
    75. f = document.body.appendChild(iframe);
    76. iframe.id = "myIframe";
    77. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
    78. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
    79. w = f.contentWindow || f.contentDocument;
    80. doc = f.contentDocument || f.contentWindow.document;
    81. doc.open();
    82. doc.write(content);
    83. doc.close();
    84. var _this = this
    85. iframe.onload = function(){
    86. _this.toPrint(w);
    87. setTimeout(function () {
    88. document.body.removeChild(iframe)
    89. }, 100)
    90. }
    91. },
    92. toPrint: function (frameWindow) {
    93. try {
    94. setTimeout(function () {
    95. frameWindow.focus();
    96. try {
    97. if (!frameWindow.document.execCommand('print', false, null)) {
    98. frameWindow.print();
    99. }
    100. } catch (e) {
    101. frameWindow.print();
    102. }
    103. frameWindow.close();
    104. }, 10);
    105. } catch (err) {
    106. console.log('err', err);
    107. }
    108. },
    109. isDOM: (typeof HTMLElement === 'object') ?
    110. function (obj) {
    111. return obj instanceof HTMLElement;
    112. } :
    113. function (obj) {
    114. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
    115. }
    116. };
    117. export default MyPlugin

    如何使用

    以 原生 为例:

    1. <div ref="printPage">xxxxx</div>
    2. <button onclick="printPage()">打印</button>
    1. import print from "/src/utils/print";
    2. printPage() {
    3. print(document.getElementById("printPage"))
    4. },

    以 Vue 为例:

    1. <div ref="printPage">xxxxx</div>
    2. <el-button @click="printPage">打印</el-button>
    1. import print from "@/utils/print";
    2. printPage() {
    3. print(this.$refs.printPage)
    4. },

    from "@/utils/print" “@”在这里是/src目录
    this.$refs.xxx 填写的是页面绑定的属性值,这里是 ref="printPage"