打印divjs方法

方法

printdiv(printpage) {
    var css = "<style>\n" +
        "    .zbtable {\n" +
        "        border-collapse: collapse;\n" +
        "        text-align: center;\n" +
        "        width: 100%;\n" +
        "    }\n" +
        "\n" +
        "    .zbtable >>> th {\n" +
        "        padding: 10px 0px;\n" +
        "        border: 1px solid #3F3F3F;\n" +
        "        width: 15%;\n" +
        "    }\n" +
        "\n" +
        "    .zbtable >>> td {\n" +
        "        padding: 10px 0px;\n" +
        "        border: 1px solid #3F3F3F;\n" +
        "        width: 15%;\n" +
        "    }\n" +
        "    .rowStyle {\n" +
        "        font-size: 16px;\n" +
        "        margin-top: 20px;\n" +
        "    }\n" +
        "    .diBtn {\n" +
        "        margin: auto;\n" +
        "        margin-right: 26px;\n" +
        "        margin-bottom: 10px;\n" +
        "    }\n" +
        "    .el-col.el-col-8 {\n" +
        "    float: left;\n" +
        "    width: 33.3333%;\n" +
        "    box-sizing: border-box;\n" +
        "    }\n" +
        "    td {\n" +
        "    padding: 10px 0px;\n" +
        "    border: 1px solid #3F3F3F;\n" +
        "    width: 15%;\n" +
        "    }\n" +
        "    th {\n" +
        "    padding: 10px 0px;\n" +
        "    border: 1px solid #3F3F3F;\n" +
        "    width: 15%;\n" +
        "    }" +
        "</style>";
    var headstr = "<html><head><title></title>" + css + "</head><body>";
    var footstr = "</body>";
    var newstr = document.all.item(printpage).innerHTML;
    //var oldstr = document.body.innerHTML;
    var OpenWindow = window.open("", "newwin", "toolbar=no ,scrollbars=" + scroll + ",menubar=no");
    // //写成一行
    OpenWindow.document.write(headstr + newstr + footstr);
    OpenWindow.document.close();
    OpenWindow.window.print();
    OpenWindow.window.close();
}

使用

<el-button type="primary" size="small" @click="printdiv('noticeDiv')">打印</el-button>

点击按钮即可打印 id为noticeDiv的div  

 


posted @ 2019-11-21 14:08  故木  阅读(340)  评论(0编辑  收藏  举报