我想只打印页面的某些部分。我的示例代码如下:
<%@ page contentType="text/html;charset=ISO-8859-1" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Insert title here</title>
</head>
<body>
<div class="body">
Blah... blah...
</div>
<div class="printable">
contents goes here...
</div>
</body>
</html>
我需要打印<div class="printable"></div>
的内容。是否可以使用jQuery执行此操作?我应该编写这个解决方案,还是有一个插件可以帮助解决这个问题?
答案 0 :(得分:4)
<link rel="stylesheet" type="text/css" media="print" href="print.css" />
body * { display: none; }
.printable { display: block; }
window.print();
答案 1 :(得分:2)
我没有从stackoverflow得到任何好的解决方案,所以我自己发布答案..
<%@ page contentType="text/html;charset=ISO-8859-1" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Insert title here</title>
<script type="text/javascript">
printDivCSS = new String ('<link href="myprintstyle.css" rel="stylesheet" type="text/css">')
function printDiv(divId) {
window.frames["print_frame"].document.body.innerHTML=printDivCSS + document.getElementById(divId).innerHTML
window.frames["print_frame"].window.focus()
window.frames["print_frame"].window.print()
}
</head>
<body>
<div class="body">
Bla h......... blah...
</div>
<div class="printable">
contents goes here.........
</div>
<b></b> <a href=javascript:printDiv('printable')>Print This Div</a><br>
参考:How do I print part of a rendered HTML page in JavaScript?