javascript打印代码无法正常工作

时间:2014-06-13 06:53:54

标签: javascript jquery html5 css3

我的java脚本打印代码无法正常工作。我有这个代码是对话框。当用户点击打印按钮时,我正在调用print()功能。 当我的打印部分在Chrome数据和表格中打开时,我遇到的问题在预览中不会很好。

我尝试实施 code here

HTML code:

   <div class="modal-body">
                <div class="" id="mydata">
                    <div id="grid">Some Data</div>
                </div>
            </div>

<button type="button" id="outageSummary_printBtn" class="btnPrint" data-dismiss="modal" onclick="print()>Print</button>

JS代码:

function print(data, event) {
                event.preventDefault();
                printElement(document.getElementById("grid"));
            };

function printElement (elem) {
                var domClone = elem.cloneNode(true);
                var $printSection = document.getElementById("grid");
                if (!$printSection) {
                    var $printSection = document.createElement("div");
                    $printSection.id = "grid";
                    document.body.appendChild($printSection);
                } else {
                    $printSection.innerHTML = "";
                    $printSection.appendChild(domClone);
                }

                var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
                if (is_chrome == true) {
                    window.print();
                    if (window.stop) {
                        location.reload(); //triggering unload (e.g. reloading the page) makes the print dialog appear
                        window.stop(); //immediately stop reloading
                    }
                } else {
                    window.print();
                }
                return false;
            };

CSS代码:

@media screen {
    #grid {
        /*display: none;*/
    }
}

@media print {
    body * {
        visibility:hidden;
    }
    #grid, #grid * {
        visibility:visible;
    }
    #grid {
        position:absolute;
        left:0px;
        top:0px;
    }

}

我检查了很多打印功能,但我无法解决此问题。我相信这与CSS或我添加Html元素有关。

请指导!!

1 个答案:

答案 0 :(得分:0)

不幸的是我不能发表评论,但你是说它在预览中正确显示,而不是在网页上显示?

(同样来自您发布的代码,您似乎错过了以下[或按钮上的onclick功能]:

  

document.getElementById(&#34; outageSummary_printBtn&#34;)。onclick = function(){    printElement(的document.getElementById(&#34;网格&#34));   };

很抱歉,如果您故意将其留在代码段中

相关问题