window.print分页打印

function init() {
    var tt = document.getElementById("tt");
    var childNodes = tt.childNodes[0].childNodes;
    var level = 0;
    for (var i = 0; i < childNodes.length; i++) {
        var topHeight = childNodes[i].offsetTop - childNodes[i].scrollTop;
        var pageHeight = topHeight - level;
        //暂定高度为850px时进行分页,高度大于850px,则该tr被分到下一页
        //appendBefore
        if (pageHeight > 850) {
            var pageTr = document.createElement("tr");
            pageTr.style.pageBreakAfter = "always";
            var tmpTr = document.createElement("tr");
            childNodes[i].parentNode.insertBefore(tmpTr, childNodes[i]);
            childNodes[i].parentNode.insertBefore(pageTr, childNodes[i]);
            level = childNodes[i - 1].offsetTop - childNodes[i - 1].scrollTop;
            console.log("当前level为:" + topHeight + ", 当前tr索引为:" + (i + 1));
        }
    }
}

你可能感兴趣的:(window)