HTML iframe标签用法案例详解
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">一、iframe 定义和用法 </a></li><li><a href="#_label1">二、属性</a></li><li><a href="#_label2">三、示例 </a></li><ul class="second_class_ul"><li><a href="#_lab2_2_0">1、iframe框架</a></li><li><a href="#_lab2_2_1">2、iframe透明</a></li><li><a href="#_lab2_2_2">3、iframe自适应高度</a></li><li><a href="#_lab2_2_3">4、通过js输出iframe广告代码</a></li></ul></ul></div><p> iframe一般用来包含别的页面,例如我们可以在我们自己的网站页面加载别人网站的内容,为了更好的效果,可能需要使iframe透明效果,那么就需要了解更多的iframe属性。</p><p class="maodian"><a name="_label0"></a></p><h2>一、iframe 定义和用法 </h2>
<p> iframe 元素会创建包含另外一个文档的内联框架(即行内框架)。 <br />
<strong>HTML 与 XHTML 之间的差异</strong> <br />
在 HTML 4.1 Strict DTD 和 XHTML 1.0 Strict DTD 中,不支持 iframe 元素。 <br />
<strong>提示和注释: </strong><br />
提示:您可以把需要的文本放置在 <iframe> 和 </iframe> 之间,这样就可以应对无法理解 iframe 的浏览器。 <br />
iframe标签是成对出现的,以<iframe>开始,</iframe>结束 <br />
iframe标签内的内容可以做为浏览器不支持iframe标签时显示 </p>
<p class="maodian"><a name="_label1"></a></p><h2>二、属性</h2>
<p><strong>New :HTML5 中的新属性。</strong></p>
<table>
<tbody>
<tr>
<th>属性</th>
<th>值</th>
<th>描述</th>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-align.html" rel="external nofollow" >align</a></td>
<td>left<br />
right<br />
top<br />
middle<br />
bottom</td>
<td>HTML5 不支持。HTML 4.01 已废弃。 规定如何根据周围的元素来对齐 <iframe>。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-frameborder.html" rel="external nofollow" >frameborder</a></td>
<td>1<br />
0</td>
<td>HTML5 不支持。规定是否显示 <iframe> 周围的边框。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-height.html" rel="external nofollow" >height</a></td>
<td>pixels</td>
<td>规定 <iframe> 的高度。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-longdesc.html" rel="external nofollow" >longdesc</a></td>
<td>URL</td>
<td>HTML5 不支持。规定一个页面,该页面包含了有关 <iframe> 的较长描述。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-marginheight.html" rel="external nofollow" >marginheight</a></td>
<td>pixels</td>
<td>HTML5 不支持。规定 <iframe> 的顶部和底部的边距。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-marginwidth.html" rel="external nofollow" >marginwidth</a></td>
<td>pixels</td>
<td>HTML5 不支持。规定 <iframe> 的左侧和右侧的边距。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-name.html" rel="external nofollow" >name</a></td>
<td>name</td>
<td>规定 <iframe> 的名称。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-sandbox.html" rel="external nofollow" >sandbox</a>New</td>
<td>""<br />
allow-forms<br />
allow-same-origin<br />
allow-scripts<br />
allow-top-navigation</td>
<td>对 <iframe> 的内容定义一系列额外的限制。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-scrolling.html" rel="external nofollow" >scrolling</a></td>
<td>yes<br />
no<br />
auto</td>
<td>HTML5 不支持。规定是否在 <iframe> 中显示滚动条。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-seamless.html" rel="external nofollow" >seamless</a>New</td>
<td>seamless</td>
<td>规定 <iframe> 看起来像是父文档中的一部分。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-src.html" rel="external nofollow" >src</a></td>
<td>URL</td>
<td>规定在 <iframe> 中显示的文档的 URL。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-srcdoc.html" rel="external nofollow" >srcdoc</a>New</td>
<td>HTML_code</td>
<td>规定页面中的 HTML 内容显示在 <iframe> 中。</td>
</tr>
<tr>
<td><a href="http://www.runoob.com/tags/att-iframe-width.html" rel="external nofollow" >width</a></td>
<td>pixels</td>
<td>规定 <iframe> 的宽度。</td>
</tr>
</tbody>
</table>
<p class="maodian"><a name="_label2"></a></p><h2>三、示例 </h2>
<p class="maodian"><a name="_lab2_2_0"></a></p><h3>1、iframe框架</h3>
<p>代码如下:</p>
<div class="jb51code">
<pre class="brush:xhtml;">
<iframe src="https://www.test.net" width="200" height="500">
该网站使用了框架技术,但是您的浏览器不支持框架,请升级您的浏览器以便正常访问。
</iframe> </pre>
</div>
<p class="maodian"><a name="_lab2_2_1"></a></p><h3>2、iframe透明</h3>
<p>在transparentBody.htm文件的<body>标签中,我已经加入了style="background-color=transparent" 通过以下四种IFRAME的写法我想大概你对iframe背景透明效果的实现方法应该会有个清晰的了解: </p>
<p>代码如下:</p>
<div class="jb51code">
<pre class="brush:xhtml;">
<IFRAME ID="Frame1" SRC="transparentBody.htm" allowTransparency="true"></IFRAME>
<IFRAME ID="Frame2" SRC="transparentBody.htm" allowTransparency="true" STYLE="background-color: green"> </IFRAME>
<IFRAME ID="Frame3" SRC="transparentBody.htm"></IFRAME>
<IFRAME ID="Frame4" SRC="transparentBody.htm" STYLE="background-color: green"> </IFRAME> </pre>
</div>
<p>注:iframe是迫不得已才使用的,因为使用iframe会带来较多的问题,而有的浏览器可以设置将iframe当作广告屏蔽。 <br />
在最近的一个工作内容中使用了iframe,开始遇到的问题是iframe高度自适应的问题,这问题在口碑网ued团队博客中找到了解决办法,后来更遇到一个iframe透明的问题 ,通常 iframe底色会是白色,在不同浏览器下可能会有不同的颜色,如果主页面有一个整体的背景色或者背景图片的时候 ,iframe区域便会出现一个白色块,与主体页面不协调,这就需要iframe透明 <br />
通过google搜索iframe透明找到了解决办法 </p>
<p>代码如下:</p>
<div class="jb51code">
<pre class="brush:xhtml;">
<iframe src="./ads_top_tian.html" allowtransparency="true" style="background-color=transparent" title="test" frameborder="0" width="470" height="308" scrolling="no"></iframe></pre>
</div>
<p>当然前提是iframe页面中没有设置颜色 </p>
<p> </p>
<p>注:iframe透明主要是使用了 allowtransparency="true" style="background-color=transparent" </p>
<p class="maodian"><a name="_lab2_2_2"></a></p><h3>3、iframe自适应高度</h3>
<p>由于篇幅过长,大家可以移步这里查看 </p>
<p><a href="https://www.jb51.net/article/15780.htm" target="_blank">https://www.jb51.net/article/15780.htm</a></p>
<p class="maodian"><a name="_lab2_2_3"></a></p><h3>4、通过js输出iframe广告代码</h3>
<p> </p>
<div class="jb51code">
<pre class="brush:js;">
document.write('<iframe align=middle marginwidth=0 marginheight=0 src="http://img.jb51.net/imgby/468_1.htm" frameborder=no scrolling=no width=660 height=80></iframe>'); </pre>
</div>
<p><span style="background-color: initial;">有时候我们需要考虑用户的浏览器是否支持iframe标签,那么就需要如下的写法</span></p>
<p>代码如下:</p>
<div class="jb51code">
<pre class="brush:xhtml;">
<iframe frameborder="0" name="Iframe1" src="https://www.jb51.net/" width="100%" height="200">
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。
</iframe></pre>
</div>
頁:
[1]