iframe展示特定html代码

业务场景是,

从服务端以字符串的形式取回 一个页面的整个html代码,
在当前页面上展示,

当然就想到了iframe

iframe要事先存在与dom树中

而不是用createElement方法增加

整个替换<html>标签

直接上代码,不bb

const iframe = document.getElementsByClassName('placeholder')[0]
const iframedoc = iframe.contentDocument || iframe.contentWindow.document;
iframedoc.children[0].innerHTML = html // 事先拿到的html
// 这个children[0]就是<html>标签

猜你喜欢

转载自blog.csdn.net/sinat_24070543/article/details/89093346
今日推荐