第一种:


<!--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')" />

最新文章

  1. JavaMail发送邮件的小程序
  2. MySQL: Tree-Hierarchical query
  3. Android 常见工具类封装
  4. CCF真题之模板生成系统
  5. cojs QAQ的图论题 题解报告
  6. 安装sql server 2008,提示要删除SQL Server 2005 Express 工具 怎么解决?
  7. 3.commonjs模块
  8. chrome开发工具指南(四)
  9. UVa225,Golygons
  10. nginx配置(windows配置)
  11. ASP.NETMVC 分页
  12. Js数组去重方法总结
  13. 完整的一次 HTTP 请求响应过程(一)
  14. camtasis studio 未能创建视频内存资源。
  15. visual studio build and rebuild 的区别
  16. memset初始化数组的问题
  17. bonjour browser 下载
  18. Linux wget命令详解
  19. 强烈推荐:Android史上最强大的自定义任务软件Tasker
  20. Easy前端正确删除datagrid的方式(避免直接删除索引没更新问题)

热门文章

  1. 重学C语言 -- printf,scanf
  2. 如何让sudo命令不需要输入密码就可执行
  3. java学习:用反射构造bean
  4. Spring核心框架 - AOP的原理及源码解析
  5. android:Faild to install,你的主机中的软件终止了一个连接错误解决
  6. Java Web 前端高性能优化(二)
  7. android ExpandableListActivity的使用
  8. SpringMVC与Struts2关于controller线程安全问题
  9. comm命令——
  10. VCL+FMX 双剑合壁编程