我试图使用 printElement() 函数打印动态 div html 元素,但是当我遇到问题时......我的代码如下
$("#btnPrint").click(function () {
if (ImgPath != '') {
sHtml = "<div id='dvPrint'><table>";
sHtml += "<tr><td>" + "<img src='" + ImgPath + "' border='0'/>" + "</td></tr>";
sHtml += "<tr><td>" + $('#lblTxt').html() + "</td></tr>";
sHtml += "</table></div>";
alert($('#dvPrint').html());
$("#element").printElement('#dvPrint');
}
else {
alert("Image not found for print");
}
return false;
});
当我只是提醒 alert($('#dvPrint').html());
它什么也不显示。那么 printElement() 是如何工作的。我可以将我的动态 div 附加到正文中......我只需要生成 div 并通过 jquery 打印其中的内容......我需要帮助。谢谢
最佳答案
您只是格式化了 html 字符串,并没有将其附加到 DOM。您无法使用 $('#dvPrint')
找到它。使用 $(sHtml).html()
获取格式化的 html。
结果打印将是:$(sHtml).printElement();
$("#btnPrint").click(function () {
if (ImgPath != '') {
sHtml = "<div id='dvPrint'><table>";
sHtml += "<tr><td>" + "<img src='" + ImgPath + "' border='0'/>" + "</td></tr>";
sHtml += "<tr><td>" + $('#lblTxt').html() + "</td></tr>";
sHtml += "</table></div>";
var $dvPrint = $(sHtml);
alert($dvPrint.html());
$dvPrint.printElement();
}
else {
alert("Image not found for print");
}
return false;
});
关于jquery - 如何使用 jquery 打印动态 div html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7608110/