Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将HTML动态加载到和iframe中_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript 将HTML动态加载到和iframe中

Javascript 将HTML动态加载到和iframe中,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我正在尝试将带有HTML的iframe动态添加到现有页面。我已经得到了iframe来加载和显示。但是,iframe的“主体”不显示。有什么帮助吗 <html> <head> <script> window.onload = function() { var iframe = document.createElement('iframe'); var html =

我正在尝试将带有HTML的iframe动态添加到现有页面。我已经得到了iframe来加载和显示。但是,iframe的“主体”不显示。有什么帮助吗

<html>
<head>
<script>
window.onload = function() {
                var iframe = document.createElement('iframe');
                var html =
                    '<html><head>' +
                        '<scr' + 'ipt src="https://localhost:44302/foo.js"></scr' + 'ipt>' +
                        '<scr' + 'ipt src="https://localhost:44302/bar.js"></scr' + 'ipt>' +
                    '</head>' +
                    '<body>Hi</body></html>';
                iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
                document.body.appendChild(iframe);
                console.log('iframe.contentWindow =', iframe.contentWindow);
            }
</script>
</head>
<body></body>
</html>

window.onload=函数(){
var iframe=document.createElement('iframe');
变量html=
'' +
'' +
'' +
'' +
“嗨”;
iframe.src='data:text/html;charset=utf-8',+encodeURI(html);
document.body.appendChild(iframe);
log('iframe.contentWindow=',iframe.contentWindow=';
}

对不起,没有足够的代表直接发表评论

var iframe = document.createElement('iframe');
var html =
    '<html>' + 
    '<head>' +
    '<scr' + 'ipt src="https://localhost:44302/oidc.js"></scr' + 'ipt>' +
    '<scr' + 'ipt src="https://localhost:44302/requestToken.js"></scr' + 'ipt>' +
    '</head>' +
    '<body>Hi</body>' + 
    '</html>';

iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
document.body.appendChild(iframe);
console.log('iframe.contentWindow =', iframe.contentWindow);

您还可以使用
srcdoc
属性而不是
src
来避免web工具包跨域问题。请看这里:'