Jquery 打印动态内容-在克隆中替换
我想打印一个动态添加内容的页面。我试着这样做:Jquery 打印动态内容-在克隆中替换,jquery,printing,Jquery,Printing,我想打印一个动态添加内容的页面。我试着这样做: $("body").on("click", function() { function preparePrint() { var print_window = window.open(); var print_document = $("div.container").clone(); print_document.find('.block').each(replaceWith("X"))
$("body").on("click", function() {
function preparePrint() {
var print_window = window.open();
var print_document = $("div.container").clone();
print_document.find('.block').each(replaceWith("X"));
print_window.document.open();
print_window.document.write(print_document.html());
print_window.document.close();
print_window.print();
print_window.close();
}
$("#print").click(function() {
preparePrint();
})
})
每次单击,都会克隆带有类容器的div
的内容<代码>容器
包含一些div
s和表。在克隆中,我尝试查找类为block
的元素,并将它们替换为大写的“X”block
是动态添加到某些td
s的类
然后,我打开一个新窗口,将克隆的html内容放在其中。然后我把它打印出来
到目前为止,还不错,但更换不起作用-为什么?我还尝试了
document.write
、html()
和text()
来获取其中的“X”,但没有效果。我总是以白色页面结束,因此克隆也可能有问题。您需要在each函数中公开对象,然后对其运行代码。您需要使用->print_document.find('.block').each(function(){$(this.replaceWidth(“X”))更改replaceWith功能代码>谢谢!使用您的代码(顺便说一句:有一个小的打字错误-请参见replaceWidth
需要replaceWith
)它可以工作!我还删除了
处理程序上的,因为它不需要。那么,你能把你的评论作为一个真实的答案发表出来,这样我就可以接受了吗?
print_document.find('.block').each(function(){ $(this).replaceWith("X") });