Far*_*kel 23 html javascript css printing jquery
我正在尝试将此功能用于我正在进行的项目的网站上.此函数的目的是仅(物理地)打印子div的内容,该内容巧合地称为选择器#content.
这是我到目前为止的一点点:
<script>
function printContent() {
window.open().document.write($("#content").html());
window.print();
window.close();
}
</script>
Run Code Online (Sandbox Code Playgroud)
当人们点击"打印"超链接时会触发该功能.新窗口将加载#content div的内容,该内容从另一个HTML文档解析:
<div id="content">
<br/>
<div id="Algemeen">
<h3>Algemene informatie</h3>
<fieldset id="profile">
<img id="male" src="./images/Pixers/male_icon.jpg"></img>
<img id="female" src="./images/Pixers/female_icon1.jpg"></img>
</fieldset>
</div>
<div id ="leftbox">
<div id ="veldbox"><label>BSN:</label>$!person.bsn</div>
<div id ="veldbox"><label>Voornaam: </label>$!person.first_name</div>
<div id ="veldbox"><label>Achternaam:</label>$!person.name_prefix $!person.last_name</div>
<div id ="veldbox"><label>Geslacht:</label>$!person.gender</div>
<div id ="veldbox"><label>Woonadres:</label>$!person.address</div>
<div id ="veldbox"><label>Plaatsnaam:</label>$!person.location</div>
<div id ="veldbox"><label>Provincie:</label>$!person.province</div>
<div id ="veldbox"><label>Postcode:</label>$!person.zipcode</div>
<div id ="veldbox"><label>Tel. nummer thuis:</label>$!person.h_number</div>
<div id ="veldbox"><label>Mobiel nummer:</label>$!person.mobile_nr</div>
<div id ="veldbox"><label>Burgerlijke Stand:</label>$!person.m_status</div>
<div id ="veldbox"><label>land van herkomst:</label>$!person.origin</div>
</div>
<div id="rightbox">
<div id ="veldbox"><label>Naam instantie:</label></div>
<div id ="veldbox"><label>Adres instantie:</label></div>
<div id ="veldbox"><label>Postcode instantie:</label></div>
<div id ="veldbox"><label>Tel. instantie:</label></div>
<div id ="veldbox"><label>Fax instantie:</label></div>
<div id ="veldbox"><label>E-mail instantie:</label></div>
<div id ="veldbox"><label>Website instantie:</label></div>
<div id ="veldbox"><label>-:</label></div>
<div id ="veldbox"><label>-:</label></div>
<div id ="veldbox"><label>-:</label></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
它只是不会加载样式.所有内容都将在左上角裁剪.我已经尝试通过JS链接CSS,或者只是按照另一页上的建议将其放在页面的头部.我当然可能做错了.我还没有真正尝试用JQuery来解决这个问题,所以如果你有任何其他解决方案可以帮助我解决我的问题,我很高兴并且愿意接受一些关于它们的建议.
提前致谢!
Ree*_*eno 35
在打开的窗口中构建一个完整的HTML页面并在那里引用您的CSS文件:
var win = window.open('','printwindow');
win.document.write('<html><head><title>Print it!</title><link rel="stylesheet" type="text/css" href="styles.css"></head><body>');
win.document.write($("#content").html());
win.document.write('</body></html>');
win.print();
win.close();
Run Code Online (Sandbox Code Playgroud)
cma*_*tin 12
我做了类似上面例子的事情,但发现它并不适用于所有浏览器.以下是所有主流浏览器的测试,适合我.(请记住,某些样式可能依赖于父元素,因此如果div嵌套在这些元素中,则样式在打印窗口中可能与父页面上的样式不完全相同)
function printWithCss() {
//Works with Chome, Firefox, IE, Safari
//Get the HTML of div
var title = document.title;
var divElements = document.getElementById('printme').innerHTML;
var printWindow = window.open("", "_blank", "");
//open the window
printWindow.document.open();
//write the html to the new window, link to css file
printWindow.document.write('<html><head><title>' + title + '</title><link rel="stylesheet" type="text/css" href="/Css/site-print.css"></head><body>');
printWindow.document.write(divElements);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.focus();
//The Timeout is ONLY to make Safari work, but it still works with FF, IE & Chrome.
setTimeout(function() {
printWindow.print();
printWindow.close();
}, 100);
}
Run Code Online (Sandbox Code Playgroud)
我希望从父页面中获取所有样式并进行打印.所以我想使用HEAD中的所有css 链接.
我的解决方案使用jQuery.
(function print() {
// getting the tag element I want to print
// cloning the content so it doesn't get messed
// remove all the possible scripts that could be embed
var printContents = $('body').clone().find('script').remove().end().html();
// get all <links> and remove all the <script>'s from the header that could run on the new window
var allLinks = $('head').clone().find('script').remove().end().html();
// open a new window
var popupWin = window.open('', '_blank');
// ready for writing
popupWin.document.open();
// -webkit-print-color-adjust to keep colors for the printing version
var keepColors = '<style>body {-webkit-print-color-adjust: exact !important; }</style>';
// writing
// onload="window.print()" to print straigthaway
popupWin.document.write('<html><head>' + keepColors + allLinks + '</head><body onload="window.print()">' + printContents + '</body></html>');
// close for writing
popupWin.document.close();
})();
Run Code Online (Sandbox Code Playgroud)
在页面呈现后我也遇到了CSS加载的问题,所以解决方法是读取css文件内容并将其写入新文档:
var w = window.open();
jQuery.get('/styles.css', function (data) {
w.document.write('<html><head><style>');
w.document.write(data);
w.document.write('</style></head><body>');
w.document.write($('.print-content').html());
w.document.write('</body></html>');
w.print();
w.close();
});
Run Code Online (Sandbox Code Playgroud)