开发者

printing a div using jQuery

How do I just print the 开发者_JAVA技巧article on my web page using jQuery, article is inside a div window.print() will print the entire window. So little confused with that...!


Don't use jQuery ... use css and media=print. Here is an article for reference, and here.

Basically, create a new stylesheet for what you want to show when you print, and set the media to print:

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


You could probably put the content in an iframe and call print() on that window.

A quick google gave me http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=449 which mentions you must focus() before you print(). Note that that page is a few years old.

Also see How do I print an IFrame from javascript in Safari/Chrome.


Styling html for printing is commonly achieved using a different style sheet, e.g. in your head element:

<head>
    ...

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

</head>

The style sheet style.css is used to style your html on screen, while print.css is used to style your html for print. By providing the appropriate styling in print.css you can easily show or hide html elements to achieve the output you are looking for.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜