首先要下载:
jquery.jqprint-0.3.js
jquery-migrate-1.2.1.min.js
上面两个,我的资源里面都有
主要是使用 jquery.jqprint-0.3.js
jquery-migrate-1.2.1.min.js
是因为juqery版本兼容问题,会报错Cannot read property 'opera' of undefined
问题,加入迁移辅助插件jquery-migrate-1.2.1.min.js
可解决问题
注意:打印 table 的时候 一定要把css
写成行内样式,要不打印的时候,就不会打印表格样式了。
<button onclick="dayin()">打印</button>
<div id="dayinbufen" style="margin: 0 auto">
<h2 style="margin-bottom: 20px; text-align: center">打印表格</h2>
<table
border="0"
cellpadding="0"
cellspacing="0"
class="table"
style="border-collapse: collapse; margin: 0 auto"
>
<tbody>
<tr style="background-color: #eee">
<th>名称</th>
<th>属性</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<!-- 引入JS文件 -->
<script src="../jquery.jqprint-0.3.js?v=1.0" type="text/javascript"></script>
<script
src="../jquery-migrate-1.2.1.min.js?v=1.0"
type="text/javascript"
></script>
<script>
// 按钮点击打印
function dayin() {
// id是包裹的div的ID
$("#dayinbufen").jqprint();
}
</script>