<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function do_print(id_str){
//id-str 打印区域的id
var el = document.getElementById(id_str);
var iframe = document.createElement('IFRAME');
var doc = null;
iframe.setAttribute('style','position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
// 引入打印的专有CSS样式,根据实际修改
//doc.write("<LINK rel=\"stylesheet\" type=\"text/css\" href=\"css/print.css\">");
doc.write('<div>' + el.innerHTML + '</div>');
doc.close();
iframe.contentWindow.focus();
iframe.contentWindow.print();
if (navigator.userAgent.indexOf("MSIE") > 0){
document.body.removeChild(iframe);
}
}
</script>
</head>
<body>
<!--打印区域: -->
<div id="print_box">
<div style="width: 500px; height: 500px; border: 5px solid #6b3906;">
<h1>hello kitty!</h1>
</div>
</div>
<!--// 调用打印 -->
<button onclick="javascript:do_print('print_box');">打印</button>
</body>
</html>