我可以修改此Javascript来打印Div的图像吗?

时间:2011-07-13 05:25:52

标签: javascript jquery image html printing

我的页面布局中有一个div,我想要打印。我已经完成了一些如何执行此操作的示例代码,并提出以下建议:

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

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

function Popup(data) 
{
    var mywindow = window.open('', 'print_div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>Print Window</title>');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');
    mywindow.document.close();
    mywindow.print();
    return true;
}

</script>

然后在HTML的正文中放置以下按钮:

<input type="button" value="Print Division" onclick="PrintElem('#print_div')" />

这非常适合在页面上创建基于文本的内容的快速打印,但我需要它做的是打印出页面上显示的图像。我可以更改此脚本来执行此操作吗?

1 个答案:

答案 0 :(得分:6)

你不能这样做:

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

参考:http://api.jquery.com/html/