jquery.PrintArea.min.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. (function ($) {
  2. var printAreaCount = 0;
  3. $.fn.printArea = function () {
  4. var ele = $(this);
  5. var idPrefix = "printArea_";
  6. removePrintArea(idPrefix + printAreaCount);
  7. printAreaCount++;
  8. var iframeId = idPrefix + printAreaCount;
  9. var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';
  10. iframe = document.createElement('IFRAME');
  11. $(iframe).attr({
  12. style: iframeStyle,
  13. id: iframeId
  14. });
  15. document.body.appendChild(iframe);
  16. var doc = iframe.contentWindow.document;
  17. doc.open();
  18. $(document).find("link").filter(function () {
  19. return $(this).attr("rel").toLowerCase() == "stylesheet";
  20. }).each(
  21. function () {
  22. doc.write('<link type="text/css" rel="stylesheet" href="'
  23. + $(this).attr("href") + '" >');
  24. });
  25. doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html()
  26. + '</div>');
  27. doc.close();
  28. var frameWindow = iframe.contentWindow;
  29. frameWindow.close();
  30. frameWindow.focus();
  31. frameWindow.print();
  32. }
  33. var removePrintArea = function (id) {
  34. $("iframe#" + id).remove();
  35. };
  36. })(jQuery);