HTML网页打印
来源:互联网 发布:消除音乐原声软件 编辑:程序博客网 时间:2024/04/28 06:37
function printPartOfDocument() {
this.init.apply(this, arguments)}printPartOfDocument.prototype = { init: function(o, part) { this.o = this.getId(o); this.part = this.getId(part); this.frame = ''; this.printCss = ''; var _this = this; this.addEvent(this.o, 'click', function() { _this.create() }); }, create: function() { var _this = this; if (!this.frame) { var oFrame = document.createElement('iframe'); oFrame.setAttribute('id', 'printIframe'); oFrame.style.position = 'absolute'; oFrame.style.left = '-9999px'; document.body.appendChild(oFrame); } if (!this.printCss) this.printCss = this.getPrintCss(); setTimeout(function() { _this.frame = document.getElementById('printIframe'), d = _this.frame.contentWindow.document, h = d.getElementsByTagName('head')[0], b = d.getElementsByTagName('body')[0]; for (var i = 0; i < _this.printCss.length; i++) { h.appendChild(_this.printCss[i]); } b.innerHTML = ''; b.appendChild(_this.part.cloneNode(true)); _this.frame.contentWindow.print(); }, 0); }, getPrintCss: function() { var styles = document.getElementsByTagName('head')[0].getElementsByTagName('link'), printCss = []; for (var i = 0; i < styles.length; i++) { var attr = styles[i].getAttribute('media'); if (attr == 'all' || attr == 'print') printCss.push(styles[i].cloneNode(true)); } return printCss; }, getId: function(el) { return typeof el == 'string' ? document.getElementById(el) : el }, addEvent: function(o, type, fn) { if (o.addEventListener) { o.addEventListener(type, fn, false) } else if (o.attachEvent) { o.attachEvent('on' + type, function() { fn.call(o, window.event) }) } }}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><link href="global.css" rel="stylesheet" type="text/css"/><link href="print.css" rel="stylesheet" type="text/css" media="print" /><link href="print2.css" rel="stylesheet" type="text/css" media="all" /></head><body><a href="javascript:void(0)" title="print" id="print">print</a><div id="print_content"> <h1>只会打印这个div里的内容,下面的层的内容不会被打印 </h1> <p>打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,打印测试文字,</p></div><div> <h1>这个层的内容不会被打印</h1> <p>这里的内容不会被打印,这里的内容不会被打印,这里的内容不会被打印,这里的内容不会被打印,这里的内容不会被打印,</p></div><script type="text/javascript" src="printPartOfDocument.js"></script><script type="text/javascript">new printPartOfDocument('print','print_content');</script></body></html>
为了防止遗忘特此记录
转载自:http://www.oschina.net/code/snippet_569983_11216
- HTML网页打印
- HTML网页打印实现分页打印功能
- 打印html网页,是否打印页眉页脚
- 利用javascript打印html网页内容中的指定内容
- QT调用打印机打印html网页及文件
- 在HTML网页上打印需要的内容,JS代码
- jsp/html网页传值,并打印出来
- 网页打印
- 打印网页
- 网页打印
- 网页打印
- 网页打印
- 网页打印
- A4纸网页打印 html网页页面的宽度设置成多少
- HTML 打印
- html 打印
- 网页-HTML
- 网页 html
- VC编译选项 /ML /MLd /MT /MTd /MD /MDd之间的区别
- Oracle clusterware组成概述
- java之Integer的问题
- 程序员经典面试题及答案7
- 为Jboss4配置数据库
- HTML网页打印
- view.tag 标志 view :有时候真的会出问题,拿到的根本不是那个view
- 安装sql2012 正在启动操作系统功能"NetFx3"
- 扉页---写于0x20。
- ASP.NET 读取EXCEL 单元格 无法读取 空值 不显示
- 使用Vitamio打造自己的Android万能播放器(6)——在线播放(播放列表)
- 心情14--2012.11.22
- SVN的分支、主干合并的使用说明
- 一个项目涉及到的50个Sql语句(整理版)