如何使用jQuery打印我的页面的一部分?

maa*_*aaz 6 jquery jquery-plugins

我想只打印我页面的某些部分.我的示例代码如下:

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

我需要打印的内容<div class="printable"></div>.是否可以使用jQuery执行此操作?我应该编写这个解决方案,还是有一个插件可以帮助解决这个问题?

maa*_*aaz 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>
Run Code Online (Sandbox Code Playgroud)

请参阅:如何在 JavaScript 中打印渲染的 HTML 页面的一部分?