系统程序添加打印功能
HTML 标签
</div>
<button class="btn btn-primary" type="button" id="bt" onclick="print()">打印</button>
</div>
打印的JS代码
<script>
var authJson = eval('@ViewBag.AuthJsonStr');
function print() {
var userAgent = navigator.userAgent.toLowerCase(); //取得浏览器的userAgent字符串
if (userAgent.indexOf("trident") > -1) {
alert("请使用google或者360浏览器打印");
return false;
} else if (userAgent.indexOf('msie') > -1) {
var onlyChoseAlert = simpleAlert({
"content": "请使用Google或者360浏览器打印",
"buttons": {
"确定": function () {
onlyChoseAlert.close();
}
}
})
alert("请使用google或者360浏览器打印");
return false;
} else {//其它浏览器使用lodop
var oldstr = document.body.innerHTML;
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body></html>";
//执行隐藏打印区域不需要打印的内容
document.getElementById("CostPager").style.display = "none";
var printData = document.getElementById("ToPrintTable").innerHTML; //获得 div 里的所有 html 数据
var wind = window.open("", "newwin",
"toolbar=no,scrollbars=yes,menubar=no");
wind.document.body.innerHTML = headstr + printData + footstr;
wind.print();
//打印结束后,放开隐藏内容
document.getElementById("otherpho").style.display = "block";
wind.close();
window.document.body.innerHTML = oldstr;
}
}
</script>