javascript区域打印代码

这段代码是我从Highcharts的代码中改造出来的,非常感谢Highcharts的作者,先链上Highcharts的地址http://www.highcharts.com/,(Highcharts的统计图代码非常棒,功能超强大,大家可以试试)!

下面附上js区域打印的代码

/****************************************功能代码******************************/
(function(){
window.JPrint = {
    print: function () {
        var doc = document,
        win = window,
        container = doc.getElementById("container"),//这里就是你要打印的区域的id,也可以参数传过来,你们懂的:-)
        origDisplay = [],
        origParent = container.parentNode,
        body = doc.body,
        NONE = 'none',
        childNodes = body.childNodes;
        each = function (arr, fn) {
            var i = 0,
                len = arr.length;
            for (; i < len; i++) {
                if (fn.call(arr[i], arr[i], i, arr) === false) {
                    return i;
                }
            }
        };
        each(childNodes, function (node, i) {
            if (node.nodeType === 1) {
                origDisplay[i] = node.style.display;
                node.style.display = NONE;
            }
        });
        body.appendChild(container);
        win.print();
        setTimeout(function () {
            origParent.appendChild(container);
            each(childNodes, function (node, i) {
                if (node.nodeType === 1) {
                    node.style.display = origDisplay[i];
                }
            });
        }, 1000);
    }
};
}());
/*******************************下面是用法**********************************/
JPrint.print();
posted @ 2015-03-28 15:40  郑文亮  阅读(497)  评论(0编辑  收藏  举报