jquery.printarea.js 1.0 KB

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