js打印Iframe中的内容,并且不需要预览
js代码如下:
<script type="text/javascript" language="Javascript"> function preview1() { var bdhtml = window.document.body.innerHTML; document.getElementById('PrintPath').focus(); document.getElementById('PrintPath').contentWindow.focus(); document.getElementById('PrintPath').contentWindow.print(); //window.document.body.innerHTML = bdhtml; //重新给页面内容赋值; } </script>
分析:
1.首先获取iframe标签。
2.定位到iframe 的window. 如果不能定位到iframe的window,那么最终的打印函数,都会使用当前页面的wiodow.打印的内容将会是整个页面,而不仅仅是iframe里面的内容。(该点尤为重要)
3,用定位到的window去调用打印函数。
html 代码如下:
<form id="form1" runat="server"> <div> <div align="center"> <input type="button" name="print" value="预览并打印Iframe" onclick="preview1()"/> </div> <div> <div id="PrintFileDiv" runat="server" style="600px;height:400px;overflow-y:scroll; border:1px; border-style:solid; border-color:Black; margin-left:200px;"> </div> </div> <div> fdjgdfkljgdfkljgkldfjkl</div> <div> fdjgdfkljgdfkljgkldfjkl</div> <div> fdjgdfkljgdfkljgkldfjkl</div> <div> fdjgdfkljgdfkljgkldfjkl</div> <div> fdjgdfkljgdfkljgkldfjkl</div> <div> fdjgdfkljgdfkljgkldfjkl</div> <div> fdjgdfkljgdfkljgkldfjkl</div> <iframe id="PrintPath" src="file/EULA_Branding.htm" Width="500px" height="400px" scrolling="auto"> </iframe> </div> </form> </body>