js打印的两种方法
2024-10-18 23:27:04
第一种:
<!--startprint1-->
……打印的内容放在这里……
<!--endprint1-->
//打印
function preview(DivID) {
if (oper < ) {
bdhtml = window.document.body.innerHTML; //获取当前页的html代码
sprnstr = "<!--startprint" + oper + "-->"; //设置打印开始区域
eprnstr = "<!--endprint" + oper + "-->"; //设置打印结束区域
prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + ); //从开始代码向后取html prnhtml = prnhtml.substring(, prnhtml.indexOf(eprnstr)); //从结束代码向前取html
window.document.body.innerHTML = prnhtml;
window.print();
window.document.body.innerHTML = bdhtml;
}
else {
window.print();
} }
<input type="button" value="打印" id="btnPrint" onclick="preview('ex')" />
第二种
//打印
function preview(DivID) { var disp_setting = "toolbar=yes,location=no,";
disp_setting += "directories=yes,menubar=yes,";
disp_setting += "scrollbars=yes,width=985px";
var content_vlue = document.getElementById(DivID).innerHTML;
var docprint = window.open("", "", disp_setting);
docprint.document.open();
docprint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"');
docprint.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
docprint.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">');
docprint.document.write('<head><title>My Title</title>');
docprint.document.write('<style type="text/css"> table.gridtable{font-family: verdana,arial,sans-serif;font-size: 14px;color: #123456; border-width: 1px;border-color: #123456;border-collapse: collapse; width: 985px;}');
docprint.document.write('table.gridtable th{border: 1px solid #666666;padding: 8px;background-color: #dedede;text-align: center;}');
docprint.document.write('table.gridtable td{border: 1px solid #666666;padding: 8px;color: #123455;font-size: 13px;height: 10px;background-color: #ffffff;text-align: center;}');
docprint.document.write('table.gridtable th{border: 1px solid #666666;padding: 8px;background-color: #dedede;text-align: center;}');
docprint.document.write('#JXFX div h1{background-color: #D1D1D1;width: 35%;letter-spacing: 2.5px;}');
docprint.document.write('#JXFX #getMonthWeek{width: 130px;text-align: center;float: right;}');
docprint.document.write('</style>');
docprint.document.write('</head><body onLoad="self.print()"><center>');
docprint.document.write(content_vlue);
docprint.document.write('</center></body></html>');
docprint.document.close();
docprint.focus(); }
这个可以自己设置样式,直接拼接起你想要的样式就可以了
<input type="button" value="打印" id="btnPrint" onclick="preview('ex')" />
最新文章
- JavaMail发送邮件的小程序
- MySQL: Tree-Hierarchical query
- Android 常见工具类封装
- CCF真题之模板生成系统
- cojs QAQ的图论题 题解报告
- 安装sql server 2008,提示要删除SQL Server 2005 Express 工具 怎么解决?
- 3.commonjs模块
- chrome开发工具指南(四)
- UVa225,Golygons
- nginx配置(windows配置)
- ASP.NETMVC 分页
- Js数组去重方法总结
- 完整的一次 HTTP 请求响应过程(一)
- camtasis studio 未能创建视频内存资源。
- visual studio build and rebuild 的区别
- memset初始化数组的问题
- bonjour browser 下载
- Linux wget命令详解
- 强烈推荐:Android史上最强大的自定义任务软件Tasker
- Easy前端正确删除datagrid的方式(避免直接删除索引没更新问题)