jquery - 在 MVC 项目中,为什么 jQuery print() 函数无法加载 css
问题描述
在 MVC 项目中需要打印一个局部视图。对于使用以下 jQuery 语法的打印,它的工作正常问题是无法加载 CSS、引导程序和内联。
如何为 jQuery Print 功能启用 CSS?
jQuery打印功能
<script>
$("document").ready(function (e) {
$('body').on("click", "#btnPrint", function (e) {
$.ajax({
cache: false,
async: true,
type: 'GET',
data: {},
url: '/Home/QuickReportViewPDF',
success: function (response) {
debugger;
var printWindow = window.open('', '', 'height=600,width=900');
printWindow.document.write('<html><head><title>Medical Hub</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(response);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
},
error: function (data) {
}
});
});
});
</script>
家庭控制器
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult QuickReportViewPDF()
{
return PartialView("~/Views/Home/Index.cshtml");
}
}
索引.chhtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<button id="btnPrint" class="btn btn-primary">Print Report</button>
<div class="row">
<div class="col-md-12">
<div id="first" class="redscore col-md-4" data-high="400" data-low="300" data-score="399" style="background-color: red;min-height: 20px; ">
<div class="arrow"></div>
</div>
<div id="second" class="yelowscore col-md-4" data-high="299" data-low="200" data-score="399" style="background-color: yellow;min-height: 20px;">
<div class="arrow"></div>
</div>
<div id="third" class="greenscore col-md-4" data-high="199" data-low="0" data-score="399" style="background-color: green;min-height: 20px; ">
<div class="arrow"></div>
</div>
</div>
</div>
<script>
$("document").ready(function (e) {
$('body').on("click", "#btnPrint", function (e) {
$.ajax({
cache: false,
async: true,
type: 'GET',
data: {},
url: '/Home/QuickReportViewPDF',
success: function (response) {
debugger;
var printWindow = window.open('', '', 'height=600,width=900');
printWindow.document.write('<html><head><title>Medical Hub</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(response);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
},
error: function (data) {
}
});
});
});
</script>
<style>
#wrapper {
display: flex;
/*border: 1px solid black;*/
}
#first {
background-color: red !important;
border: 1px solid black;
}
#second {
background-color: yellow !important;
border: 1px solid black;
}
#third {
background-color: green !important;
border: 1px solid black;
}
</style>
</body>
</html>
解决方案
推荐阅读
- cuda - 无论如何打印dim3值 - 网格尺寸,Cuda中的块尺寸?
- java - 如何从 JSONObject 获取下一个值
- opencv - 使用 optimize_for_inference 和 graph_transform 工具后,Inception V3 重新训练模型错误预测
- mongodb - MongoDB 和 Mongoose 正确模式
- python - 并行迭代文档行
- c# - Azure CosmosDB Mongo C# 驱动程序 ReplaceOneAsync 抛出“请求大小太大”
- python - 不支持缓存 sha2 密码 mysql
- javascript - 骨干路由器功能 goto 不存在
- function - 八度函数如何隔离值并打印它们
- python - 将输出导出到 csv 文件?