将HTML字符串显示为Iframe中的网页 [英] Display HTML String as a web page in a Iframe

查看:1177
本文介绍了将HTML字符串显示为Iframe中的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Java脚本代码。我想显示存储在 content 中的HTML字符串,我希望它显示在iframe中,而不是显示在src url中。



我该怎么做。



代码:

  if($(#wikiframe)。length == 0){
var s =;
s = getSelText();
var content =< html> + s +< / html>;
if(s ==){
var s = prompt(Forget something?); ((s!=)&(s!= null)){
$(body)。append(\


) < div id ='wikiframe'> \
< div id ='wikiframe_veil'style =''> \
< p>正在载入...< / p> \\ \\
< / div> \
< iframe src ='???'onload = \$('#wikiframe iframe')。slideDown(500); \>使用iFrames< / iframe> \
< style type ='text / css'> \
#wikiframe_veil {display:none; position:fixed; width:100%; height: 100%; top:0; left:0; background-color:rgba(255,255,255,.25); cursor:pointer; z-index:900;} \
#wikiframe_veil p {color:black; font:普通正常粗体20px / 20px Helvetica,sans-serif; position:absolute; top:50%; left:50%; wid th:10em; margin:-10px auto 0 -5em; text-align:center;} \
#wikiframe iframe {display:none;位置:固定;前10名%;左:10%;宽度:80%;身高:80%; z-index:999; border:10px solid rgba(0,0,0,.5); margin:-5px 0 0 -5px; } \
< / style> \
< / div>);
$(#wikiframe_veil)。fadeIn(750);
}

code

$ b $ getSelText
会得到字符串然后我用< html> < \ html>>

添加它。

我希望这个 content 中的Html代码显示在iframe中。



更新:



请检查JS提琴链接:我可以' t找出为什么它不能正常工作,增加了@mathew提出的修改。

解决方案

em> src 属性:



http ://en.wikipedia.org/wiki/Data_URI_scheme

Here's my Java script code. I want to display my HTML string which is stored in content, I want this to be displayed in the iframe instead it displaying from the src url.

How do I do it.

Code:

if ($("#wikiframe").length == 0) {
                var s = "";
                s = getSelText();
                var content = "<html>" + s + "</html>";
                if (s == "") {
                    var s = prompt("Forget something?");
                }
                if ((s != "") && (s != null)) {
                    $("body").append("\
                    <div id='wikiframe'>\
                        <div id='wikiframe_veil' style=''>\
                            <p>Loading...</p>\
                        </div>\
                        <iframe src='???' onload=\"$('#wikiframe iframe').slideDown(500);\">Enable iFrames.</iframe>\
                        <style type='text/css'>\
                            #wikiframe_veil { display: none; position: fixed; width: 100%; height: 100%; top: 0; left: 0; background-color: rgba(255,255,255,.25); cursor: pointer; z-index: 900; }\
                            #wikiframe_veil p { color: black; font: normal normal bold 20px/20px Helvetica, sans-serif; position: absolute; top: 50%; left: 50%; width: 10em; margin: -10px auto 0 -5em; text-align: center; }\
                            #wikiframe iframe { display: none; position: fixed; top: 10%; left: 10%; width: 80%; height: 80%; z-index: 999; border: 10px solid rgba(0,0,0,.5); margin: -5px 0 0 -5px; }\
                        </style>\
                    </div>");
                    $("#wikiframe_veil").fadeIn(750);
                }
            }

getSelText would get the string and then I am appending it with <html> and <\html>.

I want this Html code in content to be displayed in the iframe.

How do I do it.

UPDATE:

Please check this JS Fiddle link : I can't figure out why its not working . Added changes as suggested by @mathew.

解决方案

Put your code as data:uri onto src attribute:

http://en.wikipedia.org/wiki/Data_URI_scheme

这篇关于将HTML字符串显示为Iframe中的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆