局部打印插件 jquery.PrintArea.js

时间:2014-05-10 06:34:58   收藏:0   阅读:371
bubuko.com,布布扣
(function ($) {
    var printAreaCount = 0;
    $.fn.printArea = function () {
        var ele = $(this);
        var idPrefix = "printArea_";
        removePrintArea(idPrefix + printAreaCount);
        printAreaCount++;
        var iframeId = idPrefix + printAreaCount;
        var iframeStyle = ‘position:absolute;width:0px;height:0px;left:-500px;top:-500px;‘;
        iframe = document.createElement(‘IFRAME‘);
        $(iframe).attr({
            style: iframeStyle,
            id: iframeId
        });
        document.body.appendChild(iframe);
        var doc = iframe.contentWindow.document;
        $(document).find("link").filter(function () {
            return $(this).attr("rel").toLowerCase() == "stylesheet";
        }).each(
                function () {
                    doc.write(‘<link type="text/css" rel="stylesheet" href="‘
                            + $(this).attr("href") + ‘" >‘);
                });
        doc.write(‘<div class="‘ + $(ele).attr("class") + ‘">‘ + $(ele).html()
                + ‘</div>‘);
        doc.close();
        var frameWindow = iframe.contentWindow;
        frameWindow.close();
        frameWindow.focus();
        frameWindow.print();
    }
    var removePrintArea = function (id) {
        $("iframe#" + id).remove();
    };
})(jQuery);
bubuko.com,布布扣

 

bubuko.com,布布扣
<input type="button" id="btnPrint" value="打印"/>
<div id="printContent">要打印的内容区域<div>

<script type="text/javascript">
$(function(){
        $("btnPrint").click(function(){ $("printContent").printArea(); });
});
</script>
bubuko.com,布布扣


原文:http://www.cnblogs.com/beijia/archive/2012/07/09/printArea.html

局部打印插件 jquery.PrintArea.js,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!