如何通过window.print()打印页面的特定部分
How to print some specific part of a page via window.print()
我正在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> </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>
所以我只想打印表格,而不是整页。
使用css隐藏不想打印的元素:
@media print {
.control-group {
display: none;
}
}
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>
您可以制作一个打印css(与@media print相同,但我认为它更干净,如果需要,您可以通过javascript禁用css include):
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
在该css中,您隐藏了所有不应该打印的元素,例如:
.wrapper, .header {display: none;}
一个可能的解决方案,也许不是最好的选择:
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
当然它有眨眼的效果,但它会起作用的。
相关文章:
- 在VanillaJS中模拟模型双向数据绑定
- 使用javascript将动态表从一个html页面打印到另一个html页
- JavaScript打印功能使日历停止工作
- 无法通过数组映射绑定
- 主干-不管怎样,检查事件以前是否绑定过
- 用于搜索的聚合物嵌套绑定
- esri javascript异步打印
- Angular:更新一次性绑定的数据
- 文本框不是从Javascript/Asp.net中的对话框中打印出来的
- 如何使用ngrepeat和双向绑定获得指令的隔离范围
- $translateProvider.useStaticFilesLoader的Angular Translate异步定
- react.js中的密钥绑定
- 使用regex的jquery keydown绑定不会验证撇号和句点
- 将事件处理程序绑定到任何可能的事件
- AngularJS指令只识别双向绑定类型
- jQuery/JavaScript在线公文包表单-打印样式表
- 如何打印嵌套对象的所有值
- Telerik rad组合框多列数据绑定
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- ng绑定或表达式不't打印