javascript - 删除 jQuery 中 iframe 中的 html 包装

标签 javascript jquery html iframe

我不知道如何删除 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/

相关文章:

javascript - setInterval() 的奇怪行为

javascript - 完整日历和 Laravel 加载错误

javascript - Mobiscroll:自动滚动到负载上的特定值(自定义轮子)

css - 为什么这些表单字段在生产中没有排列,但在开发中是完美的

javascript - 如何将用户选择的酒店房间乘以入住天数?

javascript - 如何使用 DropDown Value onClick Button 跟踪动态 url - 为什么这不起作用?

javascript - AngularJS 更新 JSON 文件

javascript - 学习 Javascript 与 jQuery

php - 为什么 Joomla 将 MVC View 分成两部分

javascript - 使用Javascript扫描本地目录,然后使用 "latest"文件更新Anchor标记