为什么我打印的数据不是表格格式

时间:2010-03-26 05:45:28

标签: javascript html

这是我的程序。当我提交打印按钮时,我可以打印数据,但它们没有以表格格式对齐我需要打印的格式...如何以正确的格式打印

<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<script type="text/javascript">

    function PrintElem(elem)
    {   
        //alert('ddd');
        Popup($(elem).text());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'mydiv', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');
        mywindow.document.close();
        mywindow.print();
        return true;
    }

</script>
</head>
<body>

<div id="mydiv">
   <table width="200" border="1">
  <tr>
    <td>sss</td>
    <td>sssss;</td>
    <td>snssbsp;</td>
  </tr>
  <tr>
    <td>ssss;</td>
    <td>sssnbsp;</td>
    <td>snbsp;</td>
  </tr>
  <tr>
    <td>snbsp;</td>
    <td>snbsp;</td>
    <td>snbsp;</td>
  </tr>
</table>

</div>


<input type="button" value="Print Div" onClick="PrintElem('#mydiv')" />

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您在元素上调用text方法,该方法返回删除了任何HTML标记的元素的纯文本内容。试试这个:

function PrintElem (elem)
{
   Popup($(elem).html());
}