有如下需求: 要打印页面之中的某个表格,而且还要自动横打(所以用scriptX等控件,因为window.print是竖打)? 解决办法:将这部分表格(或者div等)提取出来,专门放到一个新的窗口中去,然后根据这些内容自动生成一个包含打印控件iFrame, 再调用控件的打印方法打印该Ifrme; ____________________________________________________________ 动态生成iframe的代码: var ifr = document.createElement( "iframe" ); document.body.appendChild(ifr); var ifrdoc = ifr.contentWindow.document; ifrdoc.open(); ifrdoc.write( "<BODY>" ); ifrdoc.write( "<object id='factory' style='display:none' classid='clsid:1663ed61-23eb-11d2-b92f-008048fdd814' ></object> " ); ifrdoc.write( "</BODY>" ); ifrdoc.close() ; _____________________________________________________ 实现打印的函数(可直接拷贝调用): function PrintPart(controlId, isPortrait) //isPortrait =false 代表横打 { //以下先生成一个Docment Fragment, 不是必须 var sdiv = document.getElementById(controlId); var dc = document.createDocumentFragment(); var dcBd = dc.createElement( "BODY" ) for ( var i=0;i<=sdiv.childNodes.length-1;i++) { dcBd.appendChild(sdiv.childNodes[i]); } dc.appendChild(dcBd); //根据传入的内容生成一个iFrame并打印 var ifr = document.createElement( "iframe" ); document.body.appendChild(ifr); ifr.style.pixelWidth = 1; ifr.style.pixelHeight = 1; var ifrdoc = ifr.contentWindow.document ifrdoc.open(); ifrdoc.write( "<BODY>" ); ifrdoc.write( "<object id='factory' style='display:none' classid='clsid:1663ed61-23eb-11d2-b92f-008048fdd814' ></object> " ); ifrdoc.write(dc.body.innerHTML); //如果上面没有生成DocumentFragment,则用sdiv 对象的outerHtml属性 ifrdoc.write( "</BODY>" ); ifrdoc.close() ; //以下调用iframe中创建的打印控件实现打印 var fc = ifrdoc.getElementById( "factory" ); fc.printing.portrait = isPortrait; //是否横向打印 fc.printing.Print(); document.body.removeChild(ifr); } ______________________________________________________ 将以上代码稍加修改,就可以实现除打印之外的其它功能。主要的方法是document.write方法,动态生成新窗口,也类似: var ow = window.open( "" , "newwin" , "height=1, width=1" ); //top=10000,left=10000"); ow.document.write( "<BODY>" ) ow.document.write( "<object id='factory' style='display:none' classid='clsid:1663ed61-23eb-11d2-b92f-008048fdd814' ></object> " ) ow.document.write(dc.body.innerHTML); ow.document.write( "</BODY>" ); ow.document.close() ; |