【前端】JQ插件实现打印功能

1、准备工作

下载三个js文件:jquery.jqprint-0.3.js、jquery-migrate-1.2.1.min.js、jquery-1.11.3.min.js,

下载地址:https://download.csdn.net/download/qq_25285531/88492425

 2、实现效果

点击“打印指导单”,调起打印机,设置打印参数,开始打印

3、实现代码

HTML代码

<html>
<head>
	<title>患者用药指导单</title>
	<script type="text/javascript" src="/static/js/jquery-1.11.3.min.js" ></script>
	<script src="/static/js/jquery.jqprint-0.3.js"></script>	
	<script src="/static/js/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<!-- 打印区域 start -->
	<div id="printArea">
			<div style="line-height: 30px;">
				<h2 style="text-align: center; font-weight: bolder; font-size: 18px;">XXX医院</h2>
				<h3 style="text-align: center; font-weight: bolder;font-size: 16px;">患者用药指导单</h2>

                <div style="width: 97%; margin: 0 auto; height:auto; border: 1px solid #999; border-radius: 5px;">
						<div style="width:100%;padding:8px; border-radius: 5px;font-size:12px;display: flex;flex-wrap: wrap;">
                /***这里面是要打印的内容**/
                        </div>
                </div>
            </div>
     </div>
<!-- 打印区域 end -->

<div class="" style="position: fixed; left: 0px; bottom: 0px; width: 100%; height:60px; line-height:60px; text-align: center; background: #F1F3F4;">
	<button type="button" class="layui-btn layui-btn-normal" onclick="onprint()" style="margin-top:10px; background:#1D99D4">打印指导单</button>
</div>
</body>
</html>

 JS代码

function onprint() {

	$("#printArea").jqprint();
}

说明: 

1、在id为 "printArea" 的div中包含的是要打印的内容。

2、css样式必须写在行内,否则打印的时候没有样式。

3、jquery-migrate-1.2.1.min.js是为了兼容低版本的jquery ,比如 使用 1.7 版本的 jquery 却不会报错,能正常使用。 经过一番搜索,发现是因为高版本(1.9版本以后)不兼容以前的,需要引入一个 jquery-migrate.min.js 来兼容,否则会报下面的错误。

猜你喜欢

转载自blog.csdn.net/qq_25285531/article/details/134165254