JavaScript+CSS控制列印格式樣本介紹_javascript技巧

來源:互聯網
上載者:User
1. 用media="print"的css來控制要列印的檔案testPrint.html中引用media為print的樣式,表示列印時該樣式才起作用
複製代碼 代碼如下:

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

/style/print.css檔案
複製代碼 代碼如下:

.noprint{display:none;}

在testPrint.html中使用print.css中的樣式,在網頁瀏覽的時候是看不出效果的,但是列印的時候會起作用,如下面這一段,加上noprint之後,在瀏覽器中仍然是現實的,但是列印的時候不顯示:
複製代碼 代碼如下:

<div class="noprint">
<input type="button" onclick="window.print();" value="列印本頁" />
</div>

當然print.css裡面的樣式你可以隨便寫,改顏色啊(彩色的映像在黑白印表機下效果不好,可以用另一種樣式列印),字型什麼的都可以,隨便發揮-----------------------------------------------------------------

2. 用JavaScript來控制

因為這樣那樣的原因,可能有的人css不太熟練,有的人JavaScript比較牛x,有時候JavaScript也是不錯的選擇
複製代碼 代碼如下:

<script type="text/javascript">
<!--
//自動在列印之前執行
window.onbeforeprint = function(){
$("#test").hide();
}

//自動在列印之後執行
window.onafterprint = function(){
$("#test").show();
}
//-->
</script>


<div id="test">這段文字不會被列印出來</div>

列印之前,會調用window.onbeforeprint函數,這時你可以隨意發揮,用你的聰明才智給html重新構造一邊,然後列印。當然列印之後一般還要弄回來,就是window.onafterprint函數了

---------------------------------------------------------------

小技巧:注意一點,列印我們都知道是window.print(),其實也可以列印架構的,如window.top.centerFrame.MainFrame.print();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.