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(); //关闭打印窗口
以上两种方法都可以