$('#abc').click(function() {
$('.test1').show();
return false;
});
(function ($) {
"use strict";
function getjQueryObject(string) {
var jqObj = $("");
try {
jqObj = $(string)
.clone();
} catch (e) {
jqObj = $("<span />")
.html(string);
}
return jqObj;
}
function printFrame(frameWindow, content, options) {
var def = $.Deferred();
try {
frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || frameWindow;
var wdoc = frameWindow.document || frameWindow.contentDocument || frameWindow;
if(options.doctype) {
wdoc.write(options.doctype);
}
wdoc.write(content);
wdoc.close();
var printed = false;
var callPrint = function () {
if(printed) {
return;
}
frameWindow.focus();
try {
if (!frameWindow.document.execCommand('print', false, null)) {
frameWindow.print();
}
$('body').focus();
} catch (e) {
frameWindow.print();
}
frameWindow.close();
printed = true;
def.resolve();
}
$(frameWindow).on("load", callPrint);
setTimeout(callPrint, options.timeout);
} catch (err) {
def.reject(err);
}
return def;
}
function printContentInIFrame(content, options) {
var $iframe = $(options.iframe + "");
var iframeCount = $iframe.length;
if (iframeCount === 0) {
$iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
.prependTo('body')
.css({
"position": "absolute",
"top": -999,
"left": -999
});
}
var frameWindow = $iframe.get(0);
return printFrame(frameWindow, content, options)
.done(function () {
setTimeout(function () {
if (iframeCount === 0) {
$iframe.remove();
}
}, 1000);
})
.fail(function (err) {
console.error("Failed to print from iframe", err);
printContentInNewWindow(content, options);
})
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
}
});
}
function printContentInNewWindow(content, options) {
var frameWindow = window.open();
return printFrame(frameWindow, content, options)
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
}
});
}
function isNode(o) {
return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
}
$.print = $.fn.print = function () {
var options, $this, self = this;
if (self instanceof $) {
self = self.get(0);
}
if (isNode(self)) {
$this = $(self);
if (arguments.length > 0) {
options = arguments[0];
}
} else {
if (arguments.length > 0) {
$this = $(arguments[0]);
if (isNode($this[0])) {
if (arguments.length > 1) {
options = arguments[1];
}
} else {
options = arguments[0];
$this = $("html");
}
} else {
$this = $("html");
}
}
var defaults = {
globalStyles: true,
mediaPrint: false,
stylesheet: null,
noPrintSelector: ".no-print",
iframe: true,
append: null,
prepend: null,
manuallyCopyFormValues: true,
deferred: $.Deferred(),
timeout: 750,
title: null,
doctype: '<!doctype html>'
};
options = $.extend({}, defaults, (options || {}));
var $styles = $("");
if (options.globalStyles) {
$styles = $("style, link, meta, base, title");
} else if (options.mediaPrint) {
$styles = $("link[media=print]");
}
if (options.stylesheet) {
$styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
}
var copy = $this.clone();
copy = $("<span/>")
.append(copy);
copy.find(options.noPrintSelector)
.remove();
copy.append($styles.clone());
if (options.title) {
var title = $("title", copy);
if (title.length === 0) {
title = $("<title />");
copy.append(title);
}
title.text(options.title);
}
copy.append(getjQueryObject(options.append));
copy.prepend(getjQueryObject(options.prepend));
if (options.manuallyCopyFormValues) {
copy.find("input")
.each(function () {
var $field = $(this);
if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
if ($field.prop("checked")) {
$field.attr("checked", "checked");
}
} else {
$field.attr("value", $field.val());
}
});
copy.find("select").each(function () {
var $field = $(this);
$field.find(":selected").attr("selected", "selected");
});
copy.find("textarea").each(function () {
var $field = $(this);
$field.text($field.val());
});
}
var content = copy.html();
try {
options.deferred.notify('generated_markup', content, copy);
} catch (err) {
console.warn('Error notifying deferred', err);
}
copy.remove();
if (options.iframe) {
try {
printContentInIFrame(content, options);
} catch (e) {
console.error("Failed to print from iframe", e.stack, e.message);
printContentInNewWindow(content, options);
}
} else {
printContentInNewWindow(content, options);
}
return this;
};
})(jQuery);
如何运行第一个程序,等待5秒左右,然后运行jquery打印?我很难接受这个。因此,id将首先运行,然后打印将在id=“abc”之后运行。下面是正在使用的代码示例:
<div id="test">
<button id="abc" class="btn" onclick="jQuery.print(#test1)"></button>
</div>