當我通過鍵盤按下默認打印按鈕時,我想從網頁打印div
。但我不知道該怎麼做。通過按ctrl + p從頁面打印特定的div
我試着用
keypress
,但如果我按從 鍵盤任意鍵,將打印,但我想如果打印按鍵對ctrl+p
。
我嘗試類似:
$(document).bind('keypress', 'ctrl+p', printContent);
function printContent(){
var restorepage = document.body.innerHTML;
var printcontent = document.getElementById("print_div").innerHTML;
document.body.innerHTML = printcontent;
window.print();
document.body.innerHTML = restorepage;
}
HTML
<h1>My Print page</h1>
<div id="print_div">
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</div>
如果有任何人給我的想法那麼這將是對我有幫助。
在此先感謝。
爲什麼不使用的打印介質,以確保除了你想要的一切DIV CSS規則是'顯示:none'? – eggyal
我不知道這件事。如果有任何參考,請提供。 –
@FrayneKonok https://developer.mozilla.org/en-US/docs/Web/CSS/@media使用「@media print」,然後使所有非必需元素「display:none」。 –