如何更改新窗口的字体系列

how to change font family for new window

本文关键字:字体 系列 窗口 何更改 新窗口      更新时间:2023-09-26

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

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

一切工作,除了fontFamily不是Arial,但(我想)默认浏览器的衬线字体。

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

试试这些答案

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"来创建一个具有style属性的类。

最后有你的java脚本在HTML的末尾,这似乎是最好的解决方案比所有以上的操作。

您可能需要仔细检查:1)你尝试使用的真正的字体族参考是什么?2)它是默认提供的(在许多情况下,你将不得不添加字体定义资源到你的网站)

http://www.cssfontstack.com/arial

font-family:宋体;

看看这个。它将暂时转换为打印字体,然后再次返回到之前的字体样式。

<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也做同样的操作,希望这样