使用Jquery打印div内容
I want to print the content of a div using jQuery. This question is already asked in SO, but I can't find the correct (working) answer.
我想使用jQuery打印div的内容。这个问题已经提出来了,但是我找不到正确的答案。
This is is my HTML:
这是我的HTML:
<div id='printarea'>
<p>This is a sample text for printing purpose.</p>
<input type='button' id='btn' value='Print'>
</div>
<p>Do not print.</p>
Here I want to print the content of the div printarea
.
这里我要打印div printarea的内容。
I tried this:
我试着这样的:
$("#btn").click(function () {
$("#printarea").print();
});
But it gives a console error when the button is clicked:
但是当点击按钮时,它会出现一个控制台错误:
Uncaught TypeError: $(...).print is not a function
未捕获TypeError:$(…)。打印不是函数
But when I am trying to print the entire page using
但是当我试图打印整个页面时
window.print();
it is working. But I only want to print the content of a particular div. I saw the answer $("#printarea").print();
in many places , but this is not working.
它是有效的。但是我只想打印特定div的内容。我看到了答案$(“#printarea”).print();在很多地方,这是行不通的。
9 个解决方案
#1
36
Some jQuery research has failed, so I moved to JavaScript (thanks for your suggestion Anders).
一些jQuery研究失败了,所以我转向了JavaScript(感谢您的建议Anders)。
And it is working well...
它运行得很好……
HTML
HTML
<div id='DivIdToPrint'>
<p>This is a sample text for printing purpose.</p>
</div>
<p>Do not print.</p>
<input type='button' id='btn' value='Print' onclick='printDiv();'>
JavaScript
JavaScript
function printDiv()
{
var divToPrint=document.getElementById('DivIdToPrint');
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){newWin.close();},10);
}
更多相关文章
- Nivoslider(在动态ajax内容中)不会在第一次加载时加载图像
- 使用javascript或jquery将具有相同id的多个文本区域内容复制到剪
- 在内容可编辑DIV中的选定文本周围包装bb代码
- AJAX内容更改加上小变化效果
- 获取html DOM元素内容的边框
- jquery 更改angularJS input 内容导致绑定失效的解决办法
- 如何使我的AJAX内容能够被谷歌抓取?
- python 核心编程第六章课后题自己做的答案
- python将回车作为输入内容