jQuery.printArea 插件Bug
来源:互联网 发布:怎样投诉农村淘宝服务 编辑:程序博客网 时间:2024/05/16 12:50
jQuery.printArea.js 2.1版源码如下
(function ($) {
var counter = 0;var modes = { iframe: "iframe", popup: "popup" };
var defaults = { mode: modes.iframe,
popHt: 500,
popWd: 400,
popX: 200,
popY: 200,
popTitle: '',
popClose: false
};
var settings = {}; //global settings
$.fn.printArea = function (options) {
$.extend(settings, defaults, options);
counter++;
var idPrefix = "printArea_";
$("[id^=" + idPrefix + "]").remove();
var ele = getFormData($(this));
settings.id = idPrefix + counter;
var writeDoc;
var printWindow;
switch (settings.mode) {
case modes.iframe:
var f = new Iframe();
writeDoc = f.doc;
printWindow = f.contentWindow || f;
break;
case modes.popup:
printWindow = new Popup();
writeDoc = printWindow.doc;
}
writeDoc.open();
writeDoc.write(docType() + "<html>" + getHead() + getBody(ele) + "</html>");
writeDoc.close();
printWindow.focus();
printWindow.print();
if (settings.mode == modes.popup && settings.popClose)
printWindow.close();
}
function docType() {
if (settings.mode == modes.iframe || !settings.strict) return "";
var standard = settings.strict == false ? " Trasitional" : "";
var dtd = settings.strict == false ? "loose" : "strict";
return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01' + standard + '//EN" "http://www.w3.org/TR/html4/' + dtd + '.dtd">';
}
function getHead() {
var head = "<head><title>" + settings.popTitle + "</title>";
$(document).find("link")
.filter(function () {
return $(this).attr("rel").toLowerCase() == "stylesheet";
})
.filter(function () { // this filter contributed by "mindinquiring"
var media = $(this).attr("media");
if (media == undefined) {
return false;
}
else {
return (media.toLowerCase() == "" || media.toLowerCase() == "print");
}
})
.each(function () {
head += '<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >';
});
head += "</head>";
return head;
}
function getBody(printElement) {
return '<body><div class="' + $(printElement).attr("class") + '">' + $(printElement).html() + '</div></body>';
}
function getFormData(ele) {
$("input,select,textarea", ele).each(function () {
// In cases where radio, checkboxes and select elements are selected and deselected, and the print
// button is pressed between select/deselect, the print screen shows incorrectly selected elements.
// To ensure that the correct inputs are selected, when eventually printed, we must inspect each dom element
var type = $(this).attr("type");
if (type == "radio" || type == "checkbox") {
if ($(this).is(":not(:checked)")) this.removeAttribute("checked");
else this.setAttribute("checked", true);
}
else if (type == "text")
this.setAttribute("value", $(this).val());
else if (type == "select-multiple" || type == "select-one")
$(this).find("option").each(function () {
if ($(this).is(":not(:selected)")) this.removeAttribute("selected");
else this.setAttribute("selected", true);
});
else if (type == "textarea") {
var v = $(this).attr("value");
if ($.browser.mozilla) {
if (this.firstChild) this.firstChild.textContent = v;
else this.textContent = v;
}
else this.innerHTML = v;
}
});
return ele;
}
function Iframe() {
var frameId = settings.id;
var iframeStyle = 'border:0;position:absolute;width:0px;height:0px;left:0px;top:0px;';
var iframe;
try {
iframe = document.createElement('iframe');
document.body.appendChild(iframe);
$(iframe).attr({ style: iframeStyle, id: frameId, src: "" });
iframe.doc = null;
iframe.doc = iframe.contentDocument ? iframe.contentDocument : (iframe.contentWindow ? iframe.contentWindow.document : iframe.document);
}
catch (e) { throw e + ". iframes may not be supported in this browser."; }
if (iframe.doc == null) throw "Cannot find document.";
return iframe;
}
function Popup() {
var windowAttr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no";
windowAttr += ",width=" + settings.popWd + ",height=" + settings.popHt;
windowAttr += ",resizable=yes,screenX=" + settings.popX + ",screenY=" + settings.popY + ",personalbar=no,scrollbars=no";
var newWin = window.open("", "_blank", windowAttr);
newWin.doc = newWin.document;
return newWin;
}
})(jQuery);
出现的问题,点击打印的时候没有反应(正在等待的...)
经过FireDebug调试发现问题的代码如下:
function getHead() {
... ...
var media = $(this).attr("media");
return (media.toLowerCase() == "" || media.toLowerCase() == "print"); //此时media变量没有定义。因此停顿问题出现在这里。经过如下修改正常:
if (media == undefined) {
return false;
}
else {
return (media.toLowerCase() == "" || media.toLowerCase() == "print");
}
HTML <link>标签
定义和用法
media属性规定被链接文档将显示在什么设备上
media属性用于为不同的媒介类型规定不同的样式
浏览器支持
所有浏览器都支持值为"screen","print"以及"all"的media属性。
提示:在全屏模式中,opera也支持"projection"属性值
语法
<link media="value">
属性值
screen:计算机屏幕(默认)
tty:电传打字机以及类似的使用等宽字符网络的媒介
tv:电视机类型设备(低分辨率、有限的滚屏能力)
projection:放映机
handheld:手持设备(小屏幕、有限带宽)
print:打印机预览模式/打印页面
braille:盲人点字法反馈设备
aural:语音合成器
all:适用于所有设备
- jQuery.printArea 插件Bug
- jQuery打印插件PrintArea实现jQuery打印插件PrintArea实现
- jQuery打印插件PrintArea实现
- jquery.PrintArea.js打印插件
- jquery.printarea打印插件字体设置问题
- 局部打印插件 jquery.PrintArea.js
- 局部打印插件 jquery.PrintArea.js
- jQuery插件PrintArea实现javascript打印页面某区域功能
- jquery插件printArea打印、corner圆角使用
- jquery.PrintArea.js
- jQuery.printArea css问题
- jquery.printArea改进
- printArea.js局部打印插件
- jquery的printArea打印功能
- JQuery printarea 打印空白问题解决
- jquery.PrintArea.js 打印整个页面问题
- jquery PrintArea 实现票据的套打
- jquery.PrintArea.js 实现打印局部页面
- OPENGL 的世界坐标和 纹理坐标
- gethostbyname
- DNS报文格式 A记录查询请求 分析
- “make clean” results in “No rule to make target `clean'”
- HDU 2923 Einbahnstrasse
- jQuery.printArea 插件Bug
- 如果你不想继续穷下去,就看看吧,想想吧
- string、Empty和null三者的区别
- Android应用程序注册广播接收器(registerReceiver)的过程分析
- 高德IOS真机调试LMAMapKit出错问题的解决方法(directory not found for option)
- 麻省理工学院研究的一个多核心,云操作作系统
- Android Activity的生命周期
- 线程池
- sybase 常用命令总结