在PHP CodeIgniter中,如果你想在打印当前页面时隐藏或关闭窗口,可以通过以下几种方法实现:
你可以在页面加载完成后使用JavaScript自动关闭窗口。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Print_page extends CI_Controller {
public function index() {
$this->load->view('print_view');
}
}
在print_view.php
中添加以下JavaScript代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Print Page</title>
<script type="text/javascript">
window.onload = function() {
window.print();
setTimeout(function() {
window.close();
}, 500); // 延迟500毫秒关闭窗口
};
</script>
</head>
<body>
<h1>This is the content to be printed</h1>
</body>
</html>
通过CSS可以控制打印时的样式,从而隐藏某些元素。
@media print {
body * {
visibility: hidden;
}
#print-section, #print-section * {
visibility: visible;
}
#print-section {
position: absolute;
left: 0;
top: 0;
}
}
在HTML中使用:
<div id="print-section">
<h1>This is the content to be printed</h1>
</div>
setTimeout
延迟关闭窗口,或者确保窗口是由脚本打开的。@media print
媒体查询来专门设置打印时的样式,确保所有元素都能正确显示。<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Print_page extends CI_Controller {
public function index() {
$this->load->view('print_view');
}
}
print_view.php
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Print Page</title>
<style>
@media print {
body * {
visibility: hidden;
}
#print-section, #print-section * {
visibility: visible;
}
#print-section {
position: absolute;
left: 0;
top: 0;
}
}
</style>
<script type="text/javascript">
window.onload = function() {
window.print();
setTimeout(function() {
window.close();
}, 500);
};
</script>
</head>
<body>
<div id="print-section">
<h1>This is the content to be printed</h1>
</div>
</body>
</html>
通过上述方法,你可以在PHP CodeIgniter中实现打印当前页面时隐藏或关闭窗口的功能。
领取专属 10元无门槛券
手把手带您无忧上云