简体   繁体   English

Window.print问题

[英]Window.print Issues

here is my problem I have a code that implement the window.print but the problem is when I close the window print and go back to my page my print button is not working anymore. 这是我的问题我有一个代码实现window.print但问题是当我关闭窗口打印并返回到我的页面时,我的打印按钮不再工作。

$(function(){
    $('#button1').click(function()
        {
          $('head').append('<link rel="stylesheet" href="<?php echo base_url() ?>assets/weekly/style/weekly.css" type="text/css"/>');
           var printContents = document.getElementById('data').innerHTML;
           var originalContents = document.body.innerHTML;
           document.body.innerHTML = printContents;
           window.print();
           document.body.innerHTML = originalContents;
           //window.location = document.body.innerHTML;       
           //location.reload(); 
        });
     }); 

But when I add the location.reload() (see I comment it) the print button is working again but it loads the previous data not the current data that is why I don't want to use the location.reload() . 但是,当我添加location.reload() (请参阅我注释)时,打印按钮再次起作用,但它加载的是先前的数据,而不是当前数据,这就是我不想使用location.reload() Is there other solution or approach to my problem? 我的问题还有其他解决方案或方法吗?

You should really be using a print-specific CSS stylesheet for this, but if you want to accomplish your goal in that way, maybe this would work better: 你应该真的使用特定于打印的CSS样式表,但是如果你想以这种方式实现你的目标,那么这可能会更好:

$(function(){
    $('#button1').click(function(){
        $('head').append('<link rel="stylesheet" href="<?php echo base_url() ?>assets/weekly/style/weekly.css" type="text/css"/>');
         var printContents = $( $('#data').html() );
         var originalContents = $('body > *').hide();
         $('body').append( printContents );
         window.print();
         printContents.remove();
         originalContents.show();
      });
 }); 

Make a new element using the innerHTML of the 'data' element. 使用'data'元素的innerHTML创建一个新元素。 Hide all the children of the body tag. 隐藏body标签的所有子项。 Append the new element to the body tag. 将新元素附加到body标记。 Print. 打印。 Remove the new element. 删除新元素。 Show the original contents again. 再次显示原始内容。

This is still kind of messy, but you shouldn't lose your events. 这仍然有点混乱,但你不应该失去你的活动。

I fixed a similar problem by just opening a new window with only the content I wanted to print like this: 我修复了一个类似的问题,只需打开一个新窗口,其中只包含我想要打印的内容,如下所示:

function printPage(htmlstring){

    var boiler  = "put any styling or js scripts here to make it look right";
        boiler += "<div id='print-page-content'>\n</div>\n";

    var PrintWindow = window.open("","newwin", height=600, width=800, toolbar=no, menubar=no");
    PrintWindow.document.write(boiler);
    $(PrintWindow.document).find("#print-page-content").append(htmlstring);
    $(PrintWindow).ready(function(){
        PrintWindow.print();
    });
}

Then just call it by: 然后通过以下方式调用它:

printPage(document.body.innerHTML);

Thank you very much for all the answers and the response guys. 非常感谢您的所有答案和回复人员。 I really appreciate. 我真的很感激。 I just want to add answer this one is came from my friend. 我只是想补充一下这个来自我朋友的回答。

First on the button add onlick="window.print()" 首先在按钮上添加onlick =“window.print()”

 <p><img src="images/printButton.gif" id="button1" width="100" height="75" onclick="window.print()"></p>

And then add css style like this 然后像这样添加css样式

<link href = 'css/print.css' rel = 'stylesheet' style = 'text/css'  MEDIA="print, handheld"/>

Don't forget the MEDIA="print,handheld" 不要忘记MEDIA =“打印,手持”

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

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