php
  • javascript
  • 2013-04-04 108 views 2 likes 
    2

    我想從網頁中打印出來。我已經爲此編寫了代碼。此代碼是:如何使用php打印出網頁的某些部分?

    function printPage(){ 
         var tableData = '<table border="1">'+document.getElementsByTagName('table')[0].innerHTML+'</table>'; 
         var data = '<button onclick="window.print()">Print this page</button><br/>'+tableData;  
         myWindow=window.open('','','width=500,height=600'); 
         myWindow.innerWidth = screen.width; 
         myWindow.innerHeight = screen.height; 
         myWindow.screenX = 0; 
         myWindow.screenY = 0; 
         myWindow.document.write(data); 
         myWindow.focus(); 
        }; 
    

    但我不想'打印此頁'按鈕打印出來。

    回答

    2

    可以使用@media CSS樣式方法:

    @media print { 
    /* css here to take out the "print this */ 
    } 
    

    當您打印這將只適用的CSS樣式。

    例如,你可以給你的按鈕一類,說class="printbtn"

    則:

    @media print { 
    
        .printbtn { display: none; } 
    
    } 
    
    +1

    更好地添加C SS與樣式表,但如果你必須按照你的問題用javascript來做,只需添加以下內容: var css = myWindow.document.createElement('style'); css.type =「text/css」; css.innerHTML =「@media print {button {display:none;}}」; myWindow.document.body.appendChild(css); – thefrontender 2013-04-04 05:59:38

    +0

    它正在工作。謝謝。 – 2013-04-04 06:05:21

    +0

    謝謝@thefrontender :)忘了提及那部分。 – LazerSharks 2013-04-04 19:10:54

    0

    只需使用打印定義面積CSS ....

    <style type="text/css"> 
    
        #printit { display: block; } 
    
        @media print 
        { 
         #dont-printit { display: none; } 
         #printit { display: block; } 
        } 
        </style> 
    

    和使用<div id="printit">打印網頁區域的部分......

    +0

    歡迎您:) – Dinesh 2013-04-04 12:20:34

    相關問題