【问题标题】:How to create iframe with document in javascript? [duplicate]如何在 javascript 中创建带有文档的 iframe? [复制]
【发布时间】:2014-02-13 01:22:30
【问题描述】:

我想在 javascript 中创建一个 iframe 元素,它有一个文档和一个 <head><body>

dom中不能存在iframe。这部分很关键。

以下尝试无效:

var frame = $('<iframe />'); // [<iframe>​</iframe>​]

var doc = document.implementation.createHTMLDocument() // #document

// TypeError: Cannot read property 'ownerDocument' of null
frame.append(doc);

// HierarchyRequestError: Failed to execute 'appendChild' on 'Node': Nodes of type '#document' may not be inserted inside nodes of type 'IFRAME'.
frame.get(0).appendChild(doc)

我也尝试在 dom 中创建它,然后尝试克隆它,但是克隆的 iframe 不再包含文档。

【问题讨论】:

    标签: javascript jquery html iframe


    【解决方案1】:

    你能用这个吗?它会将内容添加到您的 iframe

    frame.contents().find('html').html("content"); //content: content of your document
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多