javascript – window.print()CSS未在打印预览中加载
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – window.print()CSS未在打印预览中加载,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4486字,纯文字阅读大概需要7分钟。
内容图文
![javascript – window.print()CSS未在打印预览中加载](/upload/InfoBanner/zyjiaocheng/736/0ca228529393408d9772dd62684bd476.jpg)
我需要提供打印部分网页的选项.这是使用javascript window.print()实现的回答:Bill Paetzke.
弹出窗口打开(这包含CSS样式),打印对话框也会打开.但CSS样式并没有出现在印刷品中.
试过@media = print,但这也行不通.
CSS样式主要由背景颜色组成.我得到的一个选项是用1px * 1px的图像替换背景颜色并重复它.还有其他方法吗?
浏览器:IE7
码:
<html>
<head>
<style type="text/css" media="print,screen">
.hideMe{
display:block;
}
.PrintClass {
display:block;
}
.NoPrintClass{
display:block;
}
</style>
<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($('<div/>').append($(elem).clone()).html());
}
function Popup(data)
{
var mywindow;
mywindow = window.open('', 'mydiv','height=400,width=600,scrollbars=yes','');
mywindow.document.write('<html><head><title>my div</title>');
mywindow.document.write('<style type="text/css" media="print,screen">.hideMe{display:none;}.NoPrintClass{display:none;}</style>');
mywindow.document.write('</head><body>');
mywindow.document.write('drop down selected value in parent: '+mywindow.opener.document.getElementById('testSelect').options[mywindow.opener.document.getElementById('testSelect').selectedIndex].text+'<br/>');
mywindow.document.write('contentStarts<br/>');
mywindow.document.write(' using jquery: '+data);
mywindow.document.write(' using javascript: '+mywindow.opener.document.getElementById('mydiv').innerHTML);
mywindow.document.write('<br/>contentEnds');
mywindow.document.write('<br/>');
mywindow.document.write('</body></html>');
mywindow.document.focus();
mywindow.document.close();
mywindow.print();
return true;
}
</script>
</head>
<body>
This is not be printed<br/>
<div id="mydiv">This content is to be printed<br/>
<div class="hideMe"><select id="testSelect">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
<br/>
This will also be printed
<br/>print bgcolor
<div style="background:red;" class="PrintClass">print</div><br/>
<div style="background:green;" class="PrintClass">print</div><br/>
<div style="background:red;" class="NoPrintClass">not to print</div><br/>
<div style="background:green;" class="NoPrintClass">not to print</div><br/>
<div style="background:red;" class="PrintClass">print</div><br/>
<div style="background:green;" class="PrintClass">print</div><br/>
<br/> print image
<div style="background-image:url('red.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<div style="background-image:url('green.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<div style="background-image:url('red.png');background-repeat:repeat;" class="NoPrintClass">not to print</div><br/>
<div style="background-image:url('green.png');background-repeat:repeat;" class="NoPrintClass">not to print</div><br/>
<div style="background-image:url('red.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<div style="background-image:url('green.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<text>some text
<div style="position:relative;">
<img src="green.png" width="100px" height="100px" value="abcd">dfads
<div style="postion:absolute">wpeoriu</div>
</div>
</img>
</text>
</div>
<div>This will not be printed.</div>
<br/>
<div id="anotherdiv">Nor will this.</div>
<input type="button" value="Print Div" onclick="PrintElem('mydiv')" />
</body>
</html>
解决方法:
因为您使用html编写文档而没有任何样式ID
这意味着它只是在没有div的情况下编写div的内容.
用它来得到整个div
$('<div/>').append($(elem).clone()).html();
见这fiddle
HTML
<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>
<div>
This will not be printed.
</div>
<div id="anotherdiv">
Nor will this.
</div>
<input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />
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('<link rel="stylesheet" href="http://www.dynamicdrive.com/ddincludes/mainstyle.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.print();
// mywindow.close();
return true;
}
内容总结
以上是互联网集市为您收集整理的javascript – window.print()CSS未在打印预览中加载全部内容,希望文章能够帮你解决javascript – window.print()CSS未在打印预览中加载所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。