如何使用jquery打印动态div html元素
我试图使用printElement()函数打印动态div html元素但是当我遇到问题时…我的代码如下
$("#btnPrint").click(function () { if (ImgPath != '') { sHtml = ""; sHtml += "" + "" + " "; sHtml += "" + $('#lblTxt').html() + " "; sHtml += "
"; alert($('#dvPrint').html()); $("#element").printElement('#dvPrint'); } else { alert("Image not found for print"); } return false; });
当我只是提醒alert($('#dvPrint').html());
它没有任何表现。 那么printElement()如何工作。 我可以将我的动态div添加到body ….我只需要生成div并通过jquery打印其中的内容…….我需要帮助。 谢谢
你刚刚格式化了html字符串,并没有将它附加到DOM。 你无法使用$('#dvPrint')
找到它。 使用$(sHtml).html()
来获取格式化的html。
因此打印将是: $(sHtml).printElement();
$("#btnPrint").click(function () { if (ImgPath != '') { sHtml = ""; sHtml += "" + "" + " "; sHtml += "" + $('#lblTxt').html() + " "; sHtml += "
"; var $dvPrint = $(sHtml); alert($dvPrint.html()); $dvPrint.printElement(); } else { alert("Image not found for print"); } return false; });
警报($(’#dvPrint’)。html()); 在你打印这样的sHtml之前不会工作:$(’#dvPrint’)。html(sHtml) – 这会将sHtml中的所有内容都放到dvPrint中。
我更喜欢用dom创建一个div
var newdiv = document.createElement('div');
然后将它与jQuery一起使用
$(newdiv).html(".....
");
或者使用DOM的接口( .addNode()
)问候
M.