2016-09-18 28 views
0

要打印一個div的內容和有以下解決方案:我如何更改字體系列新窗口

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

一切正常,除了fontFamily不是宋體,但(我認爲)默認瀏覽器的serif字體。

回答

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」。

最後有你的java腳本在HTML的末尾,這似乎是最好的解決方案比以上所有manipulatons。

1

您可能需要仔細檢查: 1)您嘗試使用的真正的字體系列引用是什麼。 2)是由它提供的默認(在很多情況下,你將不得不字體定義資源添加到您的網站)

http://www.cssfontstack.com/arial

FONT-FAMILY:宋體,Helvetica Neue字體,黑體,無襯線;

0

看看這個,它會暫時轉換成您的字體進行打印,然後再次返回到以前的字體樣式。

<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> 

**做宋體它會工作,希望這樣

相同
相關問題