使用JS 吊旗打印机,打印页面

其实主要就是document.print()函数哈。  

function doPrint() {
        var head_str = "<html><head><title></title></head><body>";//生成头部
        var foot_str = "</body></html>";//生成尾部
        var new_str = document.getElementById('PrintView').innerHTML;//获取指定打印区域(要打印的html块)
        var old_str = document.body.innerHTML;//获得原本页面的html内容
        document.body.innerHTML = head_str + new_str + foot_str; //构建新网页
        window.print(); //打印刚才新建的网页
        document.body.innerHTML = old_str; //将网页还原
        return false;
    }
var printBox = document.getElementById('GoodsInfo'); //获取需要打印的内容
        var newWin = window.open(window.document.URL); //新打开一个标签页,这样不会影响现使用的页面
        var newContent = printBox.innerHTML;
        newWin.document.body.innerHTML = newContent; //将打印的部分赋值给新打开的标签页
        //等待2秒
        newWin.print(); //执行打印
        newWin.close(); //关闭打印窗口

 

posted on 2020-07-25 11:13  阿里正正  阅读(168)  评论(0)    收藏  举报

导航