打印不工作的DIV样式表的内容
Print Contents of a DIV stylesheet not working
无法让样式表在打印文档上工作。它在弹出窗口上工作,但在打印时不起作用。所以当屏幕上的窗口弹出时,它有适当的样式,但是当使用打印对话框时,打印窗口的内容不会从样式表中获取样式,只是默认样式。
代码如下:
<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery- 1.3.1.min.js" > </script>
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).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="main.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 id="mydiv">
This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante.
</div>
<a href="#" onclick="PrintElem('#mydiv')">Printout</a>
如果你想设计你的打印页面。它不适用于您的样式表。你需要写一个内联样式表
我也遇到过同样的问题,最后解决了,这是代码:
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" />
</head>
<body>
<div id="mydiv">
This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante.
</div>
<a onClick="PrintElem('#mydiv')" class="btn btn-default btn-icon icon-left hidden-print pull-left">
My Div
<i class="entypo-print"></i>
</a>
<script type="text/javascript">
function PrintElem(elem)
{
Popup($('<div/>').append($(elem).clone()).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('</head><body >');
myWindow.document.write(data);
myWindow.document.write('</body></html>');
myWindow.print();
myWindow.close();
return true;
};
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" > </script>
</body>
</html>
打印页通常也用于电子邮件。根据我的经验,您应该在代码(内联)中编写所有样式表代码。
相关文章:
- 将Div内容复制到文本区域或具有相同字体族样式的文本
- 使用Javascript对象为具有两个背景图像的DIV设置样式时出现问题
- 单击时更改 DIV 的样式,取消单击时再次更改
- 将元素保留在 DIV 中而不继承页面样式的最佳方法
- 带有样式显示的 Div:表格的高度错误
- 在 IE8 中将 DIV 从光标下方移动时,悬停样式保持不变
- 在 ajax 函数中向 DIV 添加样式更改
- 选择所有具有样式的 DIV 显示无
- 基于其位置的可拖动 DIV 样式
- 如何在 LI 中强制 DIV(带有溢出隐藏样式)仍然显示
- 按内联“样式”属性对 Div 进行排序
- Javascript 更改 Div 样式
- 通过嵌套类将CSS样式表的范围限定为仅一个DIV
- 制作一个Firefox插件来注入一些DIV并添加样式.那'是的
- 删除Parent Div以在不设置样式的情况下正确显示Form Input Element
- 如何在Javascript中更改两个或多个Div样式
- 更改通过Jquery和AJAX动态加载到DIV中的HTML页面元素的样式
- Onload Javascript不会改变DIV样式
- 嵌套DIV的类似斑马的css样式
- 如何将样式DIV保存为PDF ?