0

我想打印一个动态添加内容的页面。我尝试这样做:

$("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();
    })
})

每次单击时,都会克隆divwith 类的内容。持有一些s 和桌子。在克隆中,我尝试使用类查找元素并将它们替换为大写“X”。是动态添加到某些s的类。containercontainerdivblockblocktd

然后,我打开一个新窗口并将克隆的 html 内容放在那里。然后我打印它。

到目前为止,一切都很好,但替换不起作用 - 为什么?我也试过document.writehtml()text()在那里得到“X”,但没有效果。我总是得到一个白页,所以克隆也可能有问题。

4

1 回答 1

1
print_document.find('.block').each(function(){ $(this).replaceWith("X") });
于 2012-07-28T21:07:54.147 回答