媒体打印;t在chrome上应用样式,但在firefox中效果完美
media print doesn't apply style on chrome, but in firefox works perfectly
我有这个js函数:
function print(title, htmlPrint) {
var w = window.open('', '', 'width=600,height=750');
w.document.title = title;
w.document.write('<link rel="stylesheet" type="text/css" href="print.css" />');
w.document.write(htmlPrint);
w.document.close(); // needed for chrome and safari
w.print();
w.close();
return false;
}
在css中,我有这样的东西:
@media print, screen{
.....
}
在firefox作品如预期:显示标题&内容,在chrome中,它只显示标题和空白页(打印时也是如此)。
我也尝试过使用@media print{}
或@media all{}
,但仍然不起作用。
我也尝试过在<link... />
中添加media="print"
,这样做会加载css,但不会将任何css应用于页面。
我也尝试过更改函数并使用w.document.head.innerHtml
和w.document.body.innerHtml
——仍然没有,具有与.write()
相同的行为。
对我有用的东西:
首先,我必须获得css文件的完整路径,并在链接标记中设置媒体打印。
if (/chrome/i.test(navigator.userAgent)) {
w.document.write('<link rel="stylesheet" type="text/css" href="' + location.protocol + '//' + location.host + (new String(location.pathname.substring(0, location.pathname.lastIndexOf("/")))) + '/print.css" media="print" />');
}else{
w.document.write('<link rel="stylesheet" type="text/css" href="print.css" />');
}
然后它仍然不能正常工作,我刚刚发现w.close()
会给chrome带来问题。
if (/chrome/i.test(navigator.userAgent) === false) {
w.close();
}
相关文章:
- 滚动在Chrome中有效,但在Firefox或IE中无效
- jQuery自动完成在Firefox中运行良好,但在Chrome中则不然
- 为什么这个按钮在IE中有效,但在Firefox中无效
- HTML选择,在DOM中选择了正确的选项,但在firefox中显示了错误的项目
- Ajax调用在InternetExplorer中冻结UI,但在firefox中运行良好
- Javascript下载在firefox中停止,但在Chrome中有效
- 以下颜色更改功能在IE9和Firefox中运行良好,但在早期的IE或Chrome中则不然
- JavaScript函数在Safari中有效,但在Firefox中找不到
- Javascript'元素'在ie中未定义,但在chrome和firefox中运行良好
- javascript代码,用于删除firefox中的空白,但在IE和chrome中无法正常工作
- jQuery代码在Firefox中工作,但在IE中失败
- 在Firefox中工作正常,但在Chrome中不能
- Javascript键码在Firefox中不起作用,但在chrome中运行良好
- 输入类型的图像在Internet Explorer中不起作用,但在Chrome和Firefox中起作用
- 聚合物准备在IE11上启动,但在Firefox或Chrome上不启动
- 使用量角器双击在chrome中效果良好,但在firefox中不起作用
- Jquery.hide()和.show()在firefox上运行速度较慢,但在chrome上运行良好
- 禁用字段在IE中有效,但在Firefox中无效
- 在getJSON之后构建HTML在Safari中有效,但在Chrome或Firefox中无效
- 为什么对img标记的前向引用在Chrome上失败,但在FireFox和IE上有效