简体   繁体   English

javascript打印代码无法正常工作

[英]javascript print code is not working properly

My java script print code is not working properly. 我的Java脚本打印代码无法正常工作。 I have this code is dialog box. 我有这个代码是对话框。 when user clicks on print button I am calling print() function. 当用户单击打印按钮时,我正在调用print()函数。 The problem I am facing when My print section is opening in Chrome data and table are not coming fine in preview. 在Chrome中打开“我的打印”部分时,我面临的问题是数据和表格在预览中效果不佳。

I tried to implement this code here 我试图在这里实现此代码

HTML code: HTML代码:

   <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 Code : 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 Code: CSS代码:

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

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

}

I checked lot of print functionality, but I am not able to get this issue. 我检查了很多打印功能,但无法解决此问题。 I believe this is related to CSS or when I am adding Html element. 我相信这与CSS或添加HTML元素有关。

Please Guide !! 请指导!

Unfortunately I cannot post as a comment, but do you mean that it shows correctly in the preview, but not on the webpage? 不幸的是我不能发表评论,但是您是说它在预览中正确显示,但在网页上却没有显示吗?

(Also from the code you've posted, it seems you're missing the below [or an onclick function on the button]: (同样,从您发布的代码中,您似乎缺少以下内容[或按钮上的onclick函数]:

document.getElementById("outageSummary_printBtn").onclick = function () { printElement(document.getElementById("grid")); document.getElementById(“ outageSummary_printBtn”)。onclick = function(){printElement(document.getElementById(“ grid”)); }; };

Sorry if you've deliberately left this out in the snippet) 抱歉,如果您故意在代码段中忽略了此内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM