js设置打印字体

JavaScript05

js设置打印字体,第1张

在网上找到的一段代码, 你可以试下:

<script language="JavaScript" type="text/javascript">

//========================= 设置字体大中小 start =============

function doZoom(size){

var artibody = document.getElementById('artibody')

if(!artibody){

return

}

var artibodyChild = artibody.childNodes

artibody.style.fontSize = size + 'px'

//再对artibody div内的直接html节点设置fontSize属性

for(var i = 0i <artibodyChild.lengthi++){

if(artibodyChild[i].nodeType == 1){

artibodyChild[i].style.fontSize = size + 'px'

}

}

}

//========================= 设置字体大中小 end =============

//========================= 打印正文部分 start =============

//print js begin

function LoadPrintJsCallBack()

{

if(typeof forSPrint == "object" &&forSPrint.Print)

{

forSPrint.Print()

}

}

function SinaPrintGetBrowser(){

if (navigator.userAgent.indexOf("MSIE") != -1)

{

return 1

}

else if (navigator.userAgent.indexOf("Firefox") != -1)

{

return 2

}

else if (navigator.userAgent.indexOf("Navigator") != -1)

{

return 3

}

else if (navigator.userAgent.indexOf("Opera") != -1 )

{

return 4

}else

{

return 5

}

}

function LoadPrintJs()

{

var myBrowser = SinaPrintGetBrowser()

if(myBrowser == 1)

{

var js_url = "http://image2.sina.com.cn/dy/col/2007/0207/print.js"

var js = document.createElement( "script" )

js.setAttribute( "type", "text/javascript" )

js.setAttribute( "src", js_url)

js.setAttribute( "id", "sinaPrintJsUrl")

document.body.insertBefore( js, null)

document.getElementById("sinaPrintJsUrl").onreadystatechange = LoadPrintJsCallBack

}

else

{

var js_url = "http://image2.sina.com.cn/dy/col/2007/0207/print.js"

var js = document.createElement( "script" )

js.setAttribute( "type", "text/javascript" )

js.setAttribute( "src", js_url)

js.setAttribute( "id", "sinaPrintJsUrl")

js.setAttribute( "onload", "LoadPrintJsCallBack()")

document.body.insertBefore( js, null)

}

}

//print js end

//========================= 打印正文部分 end =============

//]]>

</script>

【<a href="javascript:doZoom(16)">大</a><a href="javascript:doZoom(14)">中</a><a href="javascript:doZoom(12)">小</a>】【<a href="javascript:LoadPrintJs()">打印</a>】【<a href="javascript:window.close()">关闭</a>】

汉字照常写。

报错是因为,这个js文件编码的问题,用记事本打开js文件,选择文件另存为,在另存的时候,下方有个编码的选择,选择UTF-8,即可。另存后,看看里面的汉字是不是乱码,如果乱码,就重新写一遍,重新写的是在UTF-8格式下写的,这时就不会报错了。

如果是汉字注释的话,最后使用 /*** 注释 ***/ 无论汉字怎么乱码 JS文件都不会报错