纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.5 KiB

3 years ago
  1. // -----------------------------------------------------------------------
  2. // Eros Fratini - eros@recoding.it
  3. // jqprint 0.3
  4. //
  5. // - 19/06/2009 - some new implementations, added Opera support
  6. // - 11/05/2009 - first sketch
  7. //
  8. // Printing plug-in for jQuery, evolution of jPrintArea: http://plugins.jquery.com/project/jPrintArea
  9. // requires jQuery 1.3.x
  10. //
  11. // Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  12. //------------------------------------------------------------------------
  13. (function($) {
  14. var opt;
  15. $.fn.jqprint = function (options) {
  16. opt = $.extend({}, $.fn.jqprint.defaults, options);
  17. var $element = (this instanceof jQuery) ? this : $(this);
  18. if (opt.operaSupport && $.browser.opera)
  19. {
  20. var tab = window.open("","jqPrint-preview");
  21. tab.document.open();
  22. var doc = tab.document;
  23. }
  24. else
  25. {
  26. var $iframe = $("<iframe />");
  27. if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }
  28. $iframe.appendTo("body");
  29. var doc = $iframe[0].contentWindow.document;
  30. }
  31. if (opt.importCSS)
  32. {
  33. if ($("link[media=print]").length > 0)
  34. {
  35. $("link[media=print]").each( function() {
  36. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
  37. });
  38. }
  39. else
  40. {
  41. $("link").each( function() {
  42. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
  43. });
  44. }
  45. }
  46. if (opt.printContainer) { doc.write($element.outer()); }
  47. else { $element.each( function() { doc.write($(this).html()); }); }
  48. doc.close();
  49. (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
  50. setTimeout( function() { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
  51. }
  52. $.fn.jqprint.defaults = {
  53. debug: false,
  54. importCSS: true,
  55. printContainer: true,
  56. operaSupport: true
  57. };
  58. // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
  59. jQuery.fn.outer = function() {
  60. return $($('<div></div>').html(this.clone())).html();
  61. }
  62. })(jQuery);