如何在javascript中克隆现有文档?

时间:2012-07-21 09:05:43

标签: javascript

是否可以在document中创建javascript的副本变量?

由于我是C#开发人员,我想要像

这样的东西
var doc=new document();

    var doc = document;
    doc.open('application/txt', 'replace');
    doc.charset = 'utf-8';
    doc.write('Message to write in txt file');
    doc.close();
    if (doc.execCommand('SaveAs', true,'test.txt')) {
        alert(success);
    }

我在iframe弹出窗口中有一个按钮和一些其他控件,我想在按钮上打开saveas对话框。运行上面的代码后,“在txt文件中写入消息”显示在我有按钮和其他控件的页面中

2 个答案:

答案 0 :(得分:1)

我已经解决了这个问题,如下所示。我在页面中添加了一个iframe并执行了以下操作。

    var iframe = document.getElementById('iframe');
    var doc = iframe.contentDocument;
    var message = 'Message to write in txt file';           
    doc.open('application/txt', 'replace');
    doc.charset = 'utf-8';
    doc.write(message);
    doc.close();

    doc.execCommand('SaveAs', true, 'test.txt');

答案 1 :(得分:0)

您可以在JQuery http://api.jquery.com/clone/

中使用clone方法
  

.clone()方法执行匹配集的深层副本   元素,意味着它复制匹配的元素以及所有元素   他们的后代元素和文本节点。当结合使用时   使用其中一种插入方法,.clone()是一种方便的方法   页面上的重复元素。