jQuery Print function to print div content with css

前端 未结 4 957
难免孤独
难免孤独 2020-12-31 09:38

I\'ve this function for my Wordpress plugin uses jQuery that prints the content from the div with class \"table_disp\". How can I print the css style along with it.

相关标签:
4条回答
  • 2020-12-31 09:48

    if you can add a plugin, the Jquery printThis plugin works great (since you're already using jQuery), and does exactly what you need. http://plugins.jquery.com/printThis/

    It makes an iframe and uses your page css plus allows for an additional css file specifically for printing, along with other things

    0 讨论(0)
  • 2020-12-31 09:50
    function PrintElem(elem) {
        Popup(jQuery(elem).html());
    }
    
    function Popup(data) {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
        mywindow.document.write('<html><head><title></title>');
        mywindow.document.write('<link rel="stylesheet" href="http://www.test.com/style.css" type="text/css" />');  
        mywindow.document.write('<style type="text/css">.test { color:red; } </style></head><body>');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');
        mywindow.document.close();
        mywindow.print();                        
    }
    
    <a href="#" id="hrefPrint" onclick="PrintElem('.print_div')">Print</a>
    
    0 讨论(0)
  • 2020-12-31 09:54

    You need to include the SAME stylesheet you're using in the parent page within the pop-up. You may want to make a dummy page stub/wrapper that has your stylesheet in it, then inject your HTML.

    var myStyle = '<link rel="stylesheet" href="http://mysite.com/mystyle.css" />';
    w.document.write(myStyle + jQuery('.table_disp').html());
    
    0 讨论(0)
  • 2020-12-31 10:05

    Append Stylesheet instead

    var css= '<link rel="stylesheet" href="/css/print.css" />';
                var printContent = document.getElementById("printContentID");
    
                var WinPrint = window.open('', '', 'width=900,height=650');
                WinPrint.document.write(printContent.innerHTML);
                WinPrint.document.head.innerHTML = css;
                WinPrint.document.close();
                WinPrint.focus();
                WinPrint.print();
                WinPrint.close();
    
    0 讨论(0)
提交回复
热议问题