开发者

How to print a part of my page using jQuery?

I would like to print only certain part of my page. My sample code follows:

<%@ 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>

I need to print the contents开发者_Go百科 of <div class="printable"></div>. Is it possible to do this using jQuery? Should I write this solution, or is there a plugin that would help solve this problem?


Add to HTML head:

<link rel="stylesheet" type="text/css" media="print" href="print.css" />

print.css

body * { display: none; }
.printable { display: block; }

Javascript

window.print();


i haven't get any good solution from stackoverflow so i am posting answer myself..

 <%@ 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>

refer: How do I print part of a rendered HTML page in JavaScript?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜