javascript:打开窗口并打印

javascript:打开窗口并打印,javascript,jquery,printing,Javascript,Jquery,Printing,我已经看过这个问题了:而且效果很好。我的挑战是,我想在打开的打印窗口中保留很多样式。我试过换衣服 var printWindow = window.open('', '', 'width=1000,height=700'); 到 然后像这样使用jQuery: $("#print", printWindow.document).html($("#tabContainer").html()); 但是我打开的窗口不包含实际print.aspx文件中的任何内容 有人能解释一下出了什么问题吗 编辑:

我已经看过这个问题了:而且效果很好。我的挑战是,我想在打开的打印窗口中保留很多样式。我试过换衣服

var printWindow = window.open('', '', 'width=1000,height=700');

然后像这样使用jQuery:

$("#print", printWindow.document).html($("#tabContainer").html());
但是我打开的窗口不包含实际print.aspx文件中的任何内容

有人能解释一下出了什么问题吗

编辑: 这很奇怪(至少对我来说)。。。My print.aspx具有以下功能:

<body >
    <div id="print">print content here...</div>
</body>

但是将“body”更改为“div”会使其“fail”返回0

如果可能,您应该提供完整的url,否则它会尝试使用相对url

var printWindow=window.open('','宽度=1000,高度=700')将打开一个空白url

var printWindow=window.open('print.aspx','',width=1000,height=700')打开相对于当前url的页面print.aspx。所以,如果当前url是www.google.com,它将打开www.google.com/print.aspx,这将是404页未找到

小心一些会重定向你的网站,这会让你感到困惑。例如,如果您在当前stackoverflow页面上运行此操作,它将打开此页面本身,因为
http://stackoverflow.com/questions/18633800/xxxxxx/print.aspx
将重定向到
http://stackoverflow.com/questions/18633800/xxxxxx


此外,jquery无法工作,因为您无法通过javascript将内容从一个窗口复制到另一个窗口。两者是分开的,无法沟通。

第二个在我看来很好。手动浏览到
print.aspx
时会发生什么情况?不幸的是,服务器已关闭。将在它们恢复时更新:-/好的,我确实打开了实际的Print.aspx,但我无法从调用页访问Print.aspx页面中id为=“Print”的div标记…如果div标记动态添加到原始页面,则它将不会显示在新打开的窗口中。div标记不会动态添加。它是Print.aspx文件的一部分。Print.aspx不是相对于我当前的aspx页面而不是服务器打开的吗?请参阅我对OPYes的第二条评论,它将相对于url打开。因此,如果当前url是xxxxx.aspx,那么您的新url将是xxxxx.aspx/print.aspxtried alert(printWindow.document.getElementById('print');就在window.open之后-返回null,即使my print.aspx包含此处的打印内容。。。在车身中,您是从原始窗口运行此功能的吗?
<body >
    <div id="print">print content here...</div>
</body>
alert(printWindow.document.getElementsByTagName('body').length);