如何使用jquery打印动态div html元素 [英] How to print a dynamic div html element using jquery

查看:530
本文介绍了如何使用jquery打印动态div html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用printElement()函数打印动态div html元素,但是当我遇到问题...我的代码如下

i was trying to print dynamic div html element using printElement() function but when i got problem...my code as follows

$("#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打印其中的内容即可....我需要帮助.谢谢

when i just alert alert($('#dvPrint').html()); it show nothing. then how printElement() can work. i can append my dynamic div into body....i just need to generate div and print the content inside it by jquery.......i need help. thanks

推荐答案

您只是格式化了html字符串,并且没有将其附加到DOM.您无法使用$('#dvPrint')找到它.使用$(sHtml).html()获取格式化的html.

You just formatted html string and didn't append it to the DOM. You cant find it using $('#dvPrint'). Use $(sHtml).html() to get the formatted 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打印动态div html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆