Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:50:04
Original
1239 people have browsed it

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>



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



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!