萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

jQuery 與 CSS 列印樣式控制完整指南

網頁列印樣式控制是前端常見需求。本文系統整理 CSS 列印媒體查詢、jQuery 動態控制、常見坑點與最佳實務。

CSS 列印媒體查詢三種引入方式

1. 獨立檔案(推薦)

<!-- 螢幕顯示樣式 -->
<link rel="stylesheet" href="css/screen.css" media="screen">

<!-- 列印專用樣式 -->
<link rel="stylesheet" href="css/print.css" media="print">

2. @import 規則

<style>
    @import url("css/screen.css") screen;
    @import url("css/print.css") print;
</style>

3. 單一檔案內區塊(維護方便)

<style>
    /* 螢幕共用樣式 */
    .header { color: #333; }
    .btn { display: inline-block; padding: 8px 16px; }
    
    /* 列印專用樣式 */
    @media print {
        .header { color: #000 !important; }
        .btn, .nav, .sidebar, .footer, .ads { display: none !important; }
        .content { width: 100%; max-width: none; margin: 0; padding: 0; }
        a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 0.9em; color: #666; }
        .page-break { page-break-before: always; }
        .no-break { page-break-inside: avoid; }
    }
</style>

列印樣式核心技巧

隱藏非必要元素

@media print {
    .no-print, .nav, .sidebar, .footer, .ads, .btn, .modal { display: none !important; }
}

移除背景與色彩(節省墨水)

@media print {
    * { 
        background: transparent !important; 
        color: #000 !important; 
        box-shadow: none !important;
        text-shadow: none !important;
    }
    /* 強制白背景 */
    body { background: white !important; }
}

單位轉換:pt/cm 替代 px

@media print {
    body { font-size: 12pt; line-height: 1.5; }
    h1 { font-size: 18pt; }
    .container { width: 18cm; margin: 0 auto; } /* A4 寬度約 19cm 扣除邊距 */
}

避免浮動布局破壞列印

@media print {
    .col, .float-left, .float-right { float: none !important; width: 100% !important; }
    .clearfix::after { display: block; content: ''; clear: both; }
}

分頁控制

@media print {
    /* 強制分頁:章節標題前分頁 */
    h1, h2.chapter { page-break-before: always; }
    
    /* 避免表格/圖片被截斷 */
    table, img, .chart, .card { page-break-inside: avoid; }
    
    /* 區塊結束後分頁 */
    .section-end { page-break-after: always; }
    
    /* 左右頁邊距(雙面列印) */
    @page { margin: 2cm 1.5cm; }
    @page :left { margin-right: 2cm; }
    @page :right { margin-left: 2cm; }
}

顯示連結真實 URL

@media print {
    /* 僅顯示外部連結 URL */
    a[href^="http"]::after {
        content: " (" attr(href) ")";
        font-size: 0.85em;
        color: #666;
        word-break: break-all;
    }
    /* 內部連結不顯示 */
    a[href^="/"]::after,
    a[href^="#"]::after {
        content: "";
    }
}

jQuery 動態控制列印

觸發列印對話框

$('#printBtn').on('click', function() {
    window.print();
});

動態切換列印專用樣式表

function enablePrintStylesheet() {
    // 移除現有 print 樣式表
    $('link[media="print"]').remove();
    // 新增動態產生的列印樣式
    const printCss = `
        @media print {
            .no-print { display: none !important; }
            .content { width: 100% !important; }
        }
    `;
    $('head').append(``);
}

// 列印前準備
$('#printBtn').on('click', function() {
    enablePrintStylesheet();
    // 可選:展開所有摺疊內容
    $('.collapsible').addClass('expanded');
    window.print();
});

列印特定區域(iframe 法)

function printArea(selector) {
    const content = $(selector).html();
    const iframe = $('