Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Window.print不';我不在IE工作_Javascript_Internet Explorer_Printing_Document - Fatal编程技术网

Javascript Window.print不';我不在IE工作

Javascript Window.print不';我不在IE工作,javascript,internet-explorer,printing,document,Javascript,Internet Explorer,Printing,Document,我必须打印出一个div,我正在以以下方式执行此操作: function PrintElem(elem) { Popup(elem.html()); } function Popup(data) { var mywindow = window.open('', 'to print', 'height=600,width=800'); mywindow.document.write('<html><

我必须打印出一个
div
,我正在以以下方式执行此操作:

function PrintElem(elem)
    {
        Popup(elem.html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'to print', 'height=600,width=800');
        mywindow.document.write('<html><head><title></title>');
        mywindow.document.write('<link rel="stylesheet" href="css/mycss.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.print();
        mywindow.close();

        return true;
    }
$('#print_it').click(function(){
    var element = $('#itinerario');
    PrintElem(element);
});
这里的
print\u它是按钮的id


我看到的另一件事是,一段时间后,Chrome和其他浏览器告诉我页面没有响应。为什么会发生这种情况?

您必须执行
mywindow.document.close()
,并且您可以在窗口名称中没有空格

我假设您从某个对象的onclick调用PrintElem-如果该对象是链接,则需要在onclick处理程序中返回false

如果我不得不这样做,我会这样做

function PrintElem(elem) { 
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'to_print', 'height=600,width=800');
    var html = '<html><head><title></title>'+
               '<link rel="stylesheet" href="css/mycss.css" type="text/css" />'+
               '</head><body onload="window.focus(); window.print(); window.close()">'+
               data+
               '</body></html>';
    mywindow.document.write(html);
    mywindow.document.close();
    return true;
}

我知道你已经用mplungjan关于窗口名中没有空格的注释解决了这个问题,但另一种方法是使用@media print css样式,粗略地说:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
        @media print {
            * {
                visibility: hidden;
            }

            .printMe {
                visibility: visible;
            }
        }
    </style>
    <script type="text/javascript" src="jquery-1.4.4.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Button1").click(function () {
                var divToPrint = $("#div3");
                $(divToPrint).addClass("printMe");
                window.print();
                $(divToPrint).removeClass("printMe");
            }
            )
        }
        );
    </script>
</head>
<body>
    <div id="div1">fhgjghajghhjagjdag</div>
    <div id="div2">ytiyrtiyertuyeyiyt</div>
    <div id="div3">We want to print this one.</div>
    <div id="div4">vnbcxbvmzxbvc,xbv</div>
    <div id="buttonContainer">
        <input id="Button1" type="button" value="button" />
    </div>
</body>
</html>

@媒体印刷品{
* {
可见性:隐藏;
}
.printMe{
能见度:可见;
}
}
$(文档).ready(函数(){
$(“#按钮1”)。单击(函数(){
var divToPrint=$(“#div3”);
$(divToPrint).addClass(“printMe”);
window.print();
$(divToPrint).removeClass(“printMe”);
}
)
}
);
FHGJGHJGHJAGJDAG
Ytyrtiyertuyeyiyt
我们想打印这张。
vnbcxbvmzxbvc,xbv

在我的例子中,这个问题是通过在打印前对焦来解决的

window.focus();
window.print()

在我的案例中,我只需要使用以下代码:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />


可能的重复:您不能使用
mywindow.document.innerHTML
?单击事件处理程序在哪里?已解决。。。问题是我使用了var mywindow=window.open(“”,'a name','height=600,width=800');取而代之的是var mywindow=window.open('''.'空白','高度=600,宽度=800');微软只允许像nameI这样的争论,我已经告诉过你了。没有空间。我的代码使用下划线我将使用“显示在可见性之上”,因为可见性仍然会占用页面上的空间
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />