gpt4 book ai didi

javascript - 使用不适用于 Bootstrap 的样式打印 HTML div

转载 作者:技术小花猫 更新时间:2023-10-29 11:56:58 29 4
gpt4 key购买 nike

我正在尝试使用 Javascript 打印 div 内容。我正在使用外部 css 文件进行样式设置。但是添加bootstrap.css后打印文档不应用样式。

这是我的代码。

HTML 页面

<html>
<head>
<title>Print Div Test</title>
<link href="css/mycss.css" rel="stylesheet" />
<link href="css/bootstrap.css" rel="stylesheet" />

<script src="js/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">

function PrintElem() {
Popup($('#mydiv').html());
}

function Popup(data) {
var mywindow = window.open('', 'my div', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
mywindow.document.write("<link rel=\"stylesheet\" href=\"css/bootstrap.css\" type=\"text/css\" media=\"print\" />");
mywindow.document.write("<link rel=\"stylesheet\" href=\"css/mycss.css\" type=\"text/css\" />");
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');

mywindow.print();
//mywindow.close();

return true;
}

</script>
</head>
<body>
<div>
<div id="mydiv" class="row">
<h1>This will be printed. </h1>
<div class="col-md-6">
<div style="color: red">In line styles applied.</div>
</div>
<div class="col-md-6">
<div class="myclass">Style from sheet</div>
</div>

</div>
<input type="button" value="Print Div" onclick="PrintElem()" />
</div>
</body>
</html>

CSS 文件

.myclass {
color: greenyellow;
}

是不是媒体查询的问题?在添加 bootstrap 之前,它可以与其他样式一起使用。

最佳答案

是的,这是 Bootstrap 媒体查询的“问题”。如果您检查 bootstrap css,您会看到 col-sm-xx 在 768px 之后变大,col-md-xx 在 992px 之后变大,A4 打印的视口(viewport)是 670px? (Safe Width in Pixel for Printing Web Pages?)。因此,您必须使用 col-xs-xx 进行打印或添加媒体查询以使用 col-sm-xx

@media print {
.col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6,
.col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
float: left;
}

.col-sm-12 {
width: 100%;
}

.col-sm-11 {
width: 91.66666666666666%;
}

.col-sm-10 {
width: 83.33333333333334%;
}

.col-sm-9 {
width: 75%;
}

.col-sm-8 {
width: 66.66666666666666%;
}

.col-sm-7 {
width: 58.333333333333336%;
}

.col-sm-6 {
width: 50%;
}

.col-sm-5 {
width: 41.66666666666667%;
}

.col-sm-4 {
width: 33.33333333333333%;
}

.col-sm-3 {
width: 25%;
}

.col-sm-2 {
width: 16.666666666666664%;
}

.col-sm-1 {
width: 8.333333333333332%;
}
}

可能需要在@media print 中添加更多内容

关于javascript - 使用不适用于 Bootstrap 的样式打印 HTML div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354611/

29 4 0
文章推荐: css - 如何创建一个半边框的圆圈(只有 css,不是 js 甚至 svg)?
文章推荐: html - 图片未显示在 GitHub 托管的站点中
文章推荐: html - 3个DIV彼此相邻,中间填充空白
文章推荐: html - 包含
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com