如何通过window.print()打印页面的某些特定部分

时间:2013-07-31 08:04:03

标签: php javascript html css

我正在使用php mysql中的表单。所以在我的页面中有一个表单,下面我以表格格式显示该表单的数据。现在我只想打印表结构,这就是为什么我只是将PRINT按钮作为:

<span style="float:right;"><a href="javascript:window.print()" type="button" class="btn">PRINT</a></span>

但是它会用表格字段和表格打印整个页面,我只想打印表格。 这是我整个页面的设计,包括表格和表格:

<html>
<head></head>
<body>
 <div class="container">
 <form class="form-horizontal" action="" method="post" name="userform1" id="company-form" enctype="multipart/form-data">
     <?php if($_GET[id]){?>


<fieldset>
     <legend>Add Company</legend>
    <div class="control-group">
    <label class="control-label">Company Name</label>
    <div class="controls">
    <input type="text" name="company" id="company" value="<?php echo $selup['company']?>">
    </div>
    </div>
<div class="control-group">another field</div>
<div class="control-group">another field</div>
<div class="control-group">
    <div class="controls">
    <button type="submit" class="btn" name="submit" id="submit" value="Submit">Submit</button>
    </div>
    </div>
<table class="table table-bordered">
           <tbody>
            <tr>
                <td>S.No.</td>
                <td>Company Name</td>
                <td>Type</td>
                <td>Action</td>
          </tr>
                 <?php 
                 // to print the records
                $select = "select * from company where type='Miscellaneous'";
                $query1 = mysql_query($select);  
                 while($value = mysql_fetch_array($query1)){ ?>

                <tr>
                <td><?php echo $value[id];?></td>
                <td><?php echo $value[company ];?></td>
                <td><?php echo $value[type];?></td>
                <!--<td>&nbsp;</td>-->
                <?php /*?><td><?php echo $value[amount];?></td>               
                <td><?php echo $value[date];?></td><?php */?>
                <td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $value[id];?>&cmd=edit"><i class="icon-edit"></i></a>
               <a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php  echo $value[id];?>&cmd=delete" onclick="return confirm('Are you sure you want to delete <?php  echo $value[customer];?>?')"><i class="icon-trash"></i></a></td>


                </tr><?php }?>
        </tbody>
       </table>
</fieldset>
<form>
</div>
</body>

所以我只想打印表而不是整页。

4 个答案:

答案 0 :(得分:10)

使用css隐藏您不想打印的元素:

@media print {
    .control-group {
      display: none;
    }
}

答案 1 :(得分:5)

function printContent(el){
    var restorepage = document.body.innerHTML;
    var printcontent = document.getElementById(el).innerHTML;
    document.body.innerHTML = printcontent;
    window.print();
    document.body.innerHTML = restorepage;
}
<!DOCTYPE html>
<html>
<head>

</head> 
<body> 
<h1>My page</h1>
<div id="div1">DIV 1 content...</div>
<button onclick="printContent('div1')">Print Content</button>
<div id="div2">DIV 2 content...</div>
<button onclick="printContent('div2')">Print Content</button>
<p id="p1">Paragraph 1 content...</p>
<button onclick="printContent('p1')">Print Content</button>
</body> 
</html>

答案 2 :(得分:3)

你可以制作一个print-css(和@media打印一样,但我觉得它更干净,你可以通过javascript禁用css,如果你需要的话):

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

在该css中,您隐藏了所有不应打印的元素,例如:

.wrapper, .header {display: none;}

答案 3 :(得分:2)

一种可能的解决方案,也许不是最佳选择:

1. Open a new window with JS
2. Copy the whole table into the new window (with jQuery for example)
3. Print the new window
4. Close the window

确定它有眨眼效果,但它会起作用。

相关问题