Html 使用 iframe 显示静态文本内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17389034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:37:42  来源:igfitidea点击:

Displaying static text content with iframe

htmliframe

提问by shettarvinay

I have a svery simple requirement. I have a <div>tag within which I am displaying some text content.

我有一个非常简单的要求。我有一个<div>标签,我在其中显示一些文本内容。

I want to display the same text content in an <iframe>. What I am trying to achieve I have written below. Can anyone please help?

我想在<iframe>. 我正在努力实现的目标我写在下面。任何人都可以帮忙吗?

<b>
<div>This is a content which I want to display in iframe</div>
<iframe id="iframe" src=""></iframe></b>

采纳答案by Vivek Jain

If it is a static HTML site, create a new page with the content which you want to show and set the srcof the <iframe>.

如果它是一个静态的HTML网站,创建要显示和设置的内容的新网页src<iframe>

If it is a dynamic page app (server side), create a new pageor handleror controller(whatever applies) and set the srcof the <iframe>.

如果它是一个动态的网页应用程序(服务器端),创建一个new pagehandlercontroller(无论适用),并设置src<iframe>

These are the best and simplest ways to achieve what you want to accomplish.

这些是实现您想要完成的目标的最佳和最简单的方法。

回答by shettarvinay

Found one more thing which is related to iframe which I think is interesting. You can auto fix the height and width of the iframeaccording to the content of the window. All you need to do is to include the script written below and just pass the idof the iframe.

发现另外一件与 iframe 相关的事情,我认为这很有趣。您可以iframe根据窗口的内容自动固定高度和宽度。您需要做的就是包含下面编写的脚本,然后idiframe.

<iframe src="" width="100%" id="idIframe" onload="autoResize('idIframe');" ></iframe>     

<script>
function autoResize(id)
{
    var newheight;
    var newwidth;
    if(document.getElementById)
    {
        newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
        newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
    }
    document.getElementById(id).height= (newheight) + "px";
    document.getElementById(id).width= (newwidth) + "px";
} 
</script>