Heim  >  Artikel  >  Web-Frontend  >  JavaScript打印网页指定区域的例子_javascript技巧

JavaScript打印网页指定区域的例子_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:50:041240Durchsuche

JavaScript打印页面指定div区域原理:使用window.open()在浏览器打开一个新的页面(window), 使用 window.document.write()将指定div区域的内容写入新窗口文档,document.close()关闭文档,使用window.print()调用打印机打印当前文档。

JavaScript打印函数myPrint(obj):

复制代码 代码如下:

function myPrint(obj){
    //打开一个新窗口newWindow
    var newWindow=window.open("打印窗口","_blank");
    //要打印的div的内容
    var docStr = obj.innerHTML;
    //打印内容写入newWindow文档
    newWindow.document.write(docStr);
    //关闭文档
    newWindow.document.close();
    //调用打印机
    newWindow.print();
    //关闭newWindow页面
    newWindow.close();
}

myprint()调用方法:

复制代码 代码如下:
myPrint(document.getElementById('printDivID'));

实例代码:

复制代码 代码如下:
<script><BR>function myPrint(obj){<BR> var newWindow=window.open("打印窗口","_blank");<BR> var docStr = obj.innerHTML;<BR> newWindow.document.write(docStr);<BR> newWindow.document.close();<BR> newWindow.print();<BR> newWindow.close();<BR>}<BR></script>



   打印演示区域,点击打印后会在新窗口加载这里的内容!



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn