我不知道如何删除 iframe 中的 html 包装器。
示例:
var content = '<html><body>A page</body></html>';
$('iframe').contents().find('html').html(content);
即使在开发者工具中看起来不像,上面现在有 2 个 html 标签。
var html = $('iframe').contents().find('html html').height();
当使用上面的方法时,我得到了一个高度。当选择器中只使用一个 html
时,我只得到 0。这证明有 2 个 html 标签。
如何摆脱 iframe 中的 html 包装器?
就我而言,我不会修改 content
变量。它是从其他地方获取的。
更新
我尝试过这个:
$('iframe').find('html').replaceWith(content);
但它给了我:
Failed to execute 'replaceChild' on 'Node': Nodes of type '#text' may not be inserted inside nodes of type '#document'.
最佳答案
根据我的经验,您应该使用document.write
:
var content = '<html><body>A page</body></html>';
var iframeDoc = $('iframe')[0].contentDocument; // jQuery is just for the example of course
iframeDoc.write(content);
参见-DEMO-
如果您想覆盖之前使用snippet设置的内容,则需要在调用iframeDoc.write(content);
之前调用iframeDoc.open();
。这确实没有意义,但它是......
关于javascript - 删除 jQuery 中 iframe 中的 html 包装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37590217/