javascript - 添加样式表但不添加样式

标签 javascript jquery html css

我正在尝试为我的打印页面添加一些样式(不幸的是,到目前为止我是一个初学者,所以如果我在做一些愚蠢的事情,请多多包涵)。这是样式表

@font-face {
    font-family: 'Cloister';
    src: url('../fonts/fonts/CloisterBlack.ttf');
    font-weight: normal;
    font-style: normal;
}
.navbar {
    display: none;
}
.main-navigation{
    display: none;
}
.hidden-print {
    display: none;
}
.breadcrumb {
    display: none;
}
.page-header {
    display: none;
}
.footer {
    display: none;
}
.main-container {
    /*margin: 0 ;
    padding: 0 !important;*/
}
.main-content .container{
    min-height: auto;
    border: none;
    background: #0000ff;
}
@page {
    margin: 1cm;
}

h1.page-title {
    font-family: 'Cloister';
    font-size: 40pt!important;
    text-align: center  ;
}

这是我的打印方法。

$('#printButton').click( function () {
    var divContents = $("#printPage").html();
    //console.log(divContents);
    var printWindow = window.open();
    printWindow.document.write('<html>' +
                               '<head>' +
                               '<link rel="stylesheet" href="<?= base_url() ?>assets/css/print.css" type="text/css" media="print"/>'+
                               '<title>Student Report Card</title>');
    printWindow.document.title = '';
    printWindow.document.write('<h1 class="page-title"> BeaconHouse Potohar Campus</h1>');
    printWindow.document.write('</head><body >');
    printWindow.document.write(divContents);
    printWindow.document.write('</body></html>');
    printWindow.document.close();
    printWindow.print();
});

在我检查我的打印页面之前,一切似乎都工作正常,在那里我也可以看到我的样式表被添加但是当我转到 h1 标签时,我的样式表没有出现,取而代之的是用户代理样式表(当没有样式时,由浏览器作为最后的手段添加 on stackoverflow )。

最后我添加这张图片只是为了巩固我想要传达的内容。 enter image description here

最佳答案

两个跳出三个问题:

  1. 在您的风格中,您有 font-family: 'Cloister'。那些引号不应该在那里,它应该只是 font-family: Cloister

  2. 您在 head 元素中输出 h1。 (然后浏览器会为您重新定位它。)它应该在 body 开始之后。我认为您只是将两个 document.write 行颠倒了。

  3. 作为Darren Sweeneya comment 中指出,您已将样式表标识为仅用于打印 (media="print")。使用开发工具时,如果您取消打印并检查打开的窗口而没有告诉 Chrome 向您显示“打印”媒体状态,您将不会看到该样式,因为它不适用。

以下是让 Chrome 显示“打印”媒体状态的方法:

  1. 打开 devtools(例如,右键单击 h1 并选择 Inspect)

  2. 从右上角的开发工具菜单框...

    enter image description here

    ...选择更多工具 > 渲染设置:

    enter image description here

  3. 勾选“模拟媒体”复选框并从列表中选择“打印”:

    enter image description here

关于javascript - 添加样式表但不添加样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38419758/

相关文章:

javascript - 根据外部变量将信息 block 推送到数组 X 次

javascript - ajax刷新div后SlideToggle不起作用

javascript - 获取多个多文件输入字段的文件计数

javascript - 如何等到图像在jquery中加载

html - 为什么我的文本区域的占位符没有出现?

javascript - HTML fullpage.js 不起作用

javascript - Jquery 切换两个或多个类

javascript 代码格式化程序和荧光笔

jquery - 如何使用 JQuery 将 <br> 标签放入 <td> 内容中?

javascript - Dev Tools Chrome with JQuery 你可以启用/禁用东西