function doPrint() {//取得浏览器的userAgent字符串var userAgent = navigator.userAgent.toLowerCase();if (userAgent.indexOf("trident") > -1 || userAgent.indexOf('msie') > -1) {alert("请使用google或者360浏览器打印");return false;}// 打印bdhtml = window.document.body.innerHTML;//获取当前页的html代码sprnstr = "<!--startprint-->";//设置打印开始区域eprnstr = "<!--endprint-->";//设置打印结束区域prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 17); //从开始代码向后取htmlprnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));//从结束代码向前取htmlwindow.document.body.innerHTML = prnhtml;window.print();window.document.body.innerHTML = bdhtml;}
html:<!--startprint-->//加上这两个 打印区域设置标签中间为页面内容<!--endprint--> 调用:$('#doPrint').click(function () {doPrint();})