【问题标题】:how to change font family for new window如何更改新窗口的字体系列
【发布时间】:2016-09-18 07:55:23
【问题描述】:

我想打印一个div的内容,有以下解决方案:

w=window.open();
w.document.body.style.fontFamily = 'Arial';
w.document.write(tosend);
w.print();
w.close();

除了fontFamily 不是 Arial 而是(我想)默认浏览器的衬线字体之外,一切都有效。

【问题讨论】:

    标签: javascript font-family


    【解决方案1】:

    我猜你需要一个重要的属性来为你的元素设置优先级

    试试这些答案

    w.document.body.setAttribute('style', 'font-family:Arial !important');
    

    其他

    w.document.body.style.cssText = 'font-family:Arial !important';
    

    下面这件事在 IE 中可能不起作用:

    w.document.body.style.setProperty("font-family", "Arial", "important");
    

    或者你可以附加一个内联样式

    w.document.body.appendChild(document.createTextNode('td.EvenRow a {font-family:Arial !important;}'))
    

    如果没有任何效果,则将样式属性作为重要的类并使用 body.addClass="xx"。

    终于在 HTML 的末尾添加了您的 java 脚本,这似乎是比上述所有操作最好的解决方案。

    【讨论】:

      【解决方案2】:

      您可能需要仔细检查: 1)您尝试使用的真正字体系列参考是什么。 2) 是否默认提供(在许多情况下,您必须将字体定义资源添加到您的站点)

      http://www.cssfontstack.com/arial

      字体家族:Arial,Helvetica Neue,Helvetica,sans-serif;

      【讨论】:

        【解决方案3】:

        看看这个。它会暂时转换为您的字体以供打印,然后又会恢复到以前的字体样式。

        <script>
                function print_font()
                {
                    var content = document.getElementById('Pcontent').innerHTML;
                    var win = window.open();
                    win.document.write(content);
                    win.document.body.style.fontFamily="Impact, Charcoal, sans-serif";  
                    win.print(); 
        
                }
            </script>
        
         <div id="Pcontent">
        
                <p>Print Me according to your font choice... </p>
            </div>
            <br>
            <br>
            <a href="#" onClick="print_font()" >Print</a>
        

        ** 对 arial 做同样的事情,希望如此

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多