HTML IFRAME 用法小总结

iframe是框架的一种形式,也比较常用到,下面是对其在平时常用到属性的总结

Iframe用法,下面是一个常规的列子

<iframe border=2 frameborder=0 width=500 height=500 marginheight=0 marginwidth=0 scrolling=no src="move-ad.html">
</iframe>

其中:

iframe用于设置文本或图形的浮动图文框或容器。 

border设定围绕图文框的边缘宽度 
    其中还有快读高度的设计
scrolling=no 是否有滚动条(YES,NO,AUTO) 
SRC 指定IFRAME调用的文件或图片(HTML,HTM,GIF,JPEG,JPG,PNG,TXT,*.*) 
 
一、Iframe标记的使用 
提起Iframe,可能你早已将之扔到“被遗忘的角落”了,不过,说起其兄弟Frame就不会陌生了。Frame标记即帧标记,我们所说的多帧结构就是在一个浏览器窗口中显示多个HTML文件。现在,我们遇到一种很现实的情况:如有一个教程,是一节一节地上,每页末尾做一个“上一节“、“下一节“的链接,除了每节教程内容不同之外,页面其它部分内容都是相同的,如果一页一页地做笨页面,这似乎太让人厌烦了,这时突发奇想,如果有一种方法让页面其它地方不变,只将教程做成一页一页的内容页,不含其它内容,在点击上下翻页链接时,只改变教程内容部分,其它保持不变,这样,一是省时,另则以后如教程有个三长两短的变动,也很方便,不致于牵一发而动全军了;更重要的是将那些广告Banner、栏目列表、导航等几乎每页的都有的东西只下载一次后就不再下载了。 Iframe标记,又叫浮动帧标记,你可以用它将一个HTML文档嵌入在一个HTML中显示。它不同于Frame标记最大的特征即这个标记所引用的 HTML文件不是与另外的HTML文件相互独立显示,而是可以直接嵌入在一个HTML文件中,与这个HTML文件内容相互融合,成为一个整体,另外,还可以多次在一个页面内显示同一内容,而不必重复写内容,一个形象的比喻即“画中画“电视。 
现在我们谈一下Iframe标记的使用。 
Iframe标记的使用格式是: 

<Iframe src="URL" width="x" height="x" scrolling="[OPTION]" frameborder="x"></iframe>

src:文件的路径,既可是HTML文件,也可以是文本、ASP等; 

width、height:”画中画”区域的宽与高; 
scrolling:当SRC的指定的HTML文件在指定的区域不显不完时,滚动选项,如果设置为NO,则不出现滚动条;如为Auto:则自动出现滚动条;如为Yes,则显示; 
FrameBorder:区域边框的宽度,为了让“画中画“与邻近的内容相融合,常设置为0。 
比如: 

<Iframe src="http://www.jb51.net";; width="250" height="200" scrolling="no" frameborder="0"></iframe>

 

二、父窗体与浮动帧之间的相互控制 在脚本语言与对象层次中,包含Iframe的窗口我们称之为父窗体,而浮动帧则称为子窗体,弄清这两者的关系很重要,因为要在父窗体中访问子窗体或相反都必须清楚对象层次,才能通过程序来访问并控制窗体。 
1、在父窗体中访问并控制子窗体中的对象 
在父窗体中,Iframe即子窗体是document对象的一个子对象,可以直接在脚本中访问子窗体中的对象。 
现在就有一个问题,即,我们怎样来控制这个Iframe,这里需要讲一下Iframe对象。当我们给这个标记设置了ID 属性后,就可通过文档对象模型DOM对Iframe所含的HTML进行一系列控制。 
比如在example.htm里嵌入test.htm文件,并控制test.htm里一些标记对象: 

<Iframe src="test.htm" id="test" width="250" height="200" scrolling="no" frameborder="0"></iframe>
//test.htm
<html> 
<body> 
<h1 id="myH1">hello,my boy</h1> 
</body> 
</html>

如我们要改变ID号为myH1的H1标记里的文字为hello,my dear,则可用: 
document.myH1.innerText=”hello,my dear”(其中,document可省) 
在example.htm文件中,Iframe标记对象所指的子窗体与一般的DHTML对象模型一致,对对象访问控制方式一样,就不再赘述。 
2 、在子窗体中访问并控制父窗体中对象 
在子窗体中我们可以通过其parent即父(双亲)对象来访问父窗口中的对象。 
如example.htm: 

<html> 
<body οnclick="alert(tt.myH1.innerHTML)"> 
<Iframe name="tt" src="frame1.htm" width="250" height="200" scrolling="no" frameborder="0"></iframe> 
<h1 id="myH2">hello,my wife</h1> 
</body> 
</html>

如果要在frame1.htm中访问ID号为myH2中的标题文字并将之改为”hello,my friend”,我们就可以这样写: 

parent.myH2.innerText="hello,my friend"


这里parent对象就代表当前窗体(example.htm所在窗体),要在子窗体中访问父窗体中的对象,无一例外都通过parent对象来进行。 
Iframe虽然内嵌在另一个HTML文件中,但它保持相对的独立,是一个“独立王国“哟,在单一HTML中的特性同样适用于浮动帧中。 
试想一下,通过Iframe标记,我们可将那些不变的内容以Iframe来表示,这样,不必重复写相同的内容,这有点象程序设计中的过程或函数,减省了多少繁琐的手工劳动!另外,至关重要的是,它使页面的修改更为可行,因为,不必因为版式的调整而修改每个页面,你只需修改一个父窗体的版式即可了。 
有一点要注意,Nestscape浏览器不支持Iframe标记,但在时下IE的天下,这似乎也无大碍,广泛采用Iframe标记,既为自己(网站)着了想,又为网友节省了网费,何乐而不为? 
例子 

<iframe src="页面" width="宽度" height="高度" align="排列可以是left或right,center" scrolling="是否有滚动条可以填no或yes" ></iframe> 
<IFRAME frameBorder=0 frameSpacing=0 height=25 marginHeight=0 marginWidth=0 scrolling=no name=main src="bgm/bgm.html" width=300></IFRAME>

用了iframe后 发现滚动条不漂亮 想用图片来代替? 

用下列代码替换网页的 

<title>..</title> 
<SCRIPT LANGUAGE="javascript"> 
function scroll(n) 
{temp=n; 
Out1.scrollTop=Out1.scrollTop+temp; 
if (temp==0) return; 
setTimeout("scroll(temp)",80); 
} 
</SCRIPT> 
<TABLE WIDTH="330"> 
<TR> 
<TD WIDTH="304" VALIGN="TOP" ROWSPAN="2" > 
<DIV ID=Out1 STYLE="width:100%; height:100;overflow: hidden ;border-style:dashed;border-width: 1px,1px,1px,1px;"> 
内容
内容
内容
内容
</DIV> 
</TD> 
<TD WIDTH="14" VALIGN="TOP"><IMG SRC="photo/up0605.gif" WIDTH="14" HEIGHT ="20" οnmοuseοver="scroll(-1)" οnmοuseοut="scroll(0)" οnmοusedοwn="scroll (-3)" BORDER="0" ALT="按下鼠标速度会更快!"></TD> 
</TR> 
<TR> 
<TD WIDTH="14" VALIGN="BOTTOM"><IMG SRC="photo/down0605.gif" onmouseover ="scroll(1)" οnmοuseοut="scroll(0)" οnmοusedοwn="scroll(3)" BORDER="0" WIDTH ="15" HEIGHT="21" ALT="按下鼠标速度会更快!"></TD> 
</TR> 
</TABLE>

内框架Iframe的使用 
使用Iframe可以在一人表格内调用一个外部文件,是非常有用的。本网站在很多页面上都使用了iframe效果。 
现在我们学一下Iframe标记的使用。 
Iframe标记的使用格式是: 

下面使更多的补充:
<iframe>是框架的一种形式,也比较常用到。 
  例子1。a标签的target 与 iframe 的 name
  如果一个页面里面有框架。。随便点页面里的连接,要求在这个<iframe> 里打开。在iframe 中加入name=名 
<iframe name=名  ></iframe> 
  然后再修改默认打开模式,:网页HEAD中加上<a href=URL target=名 
  例子2。 
  要插入一个页面。要求只拿中间一部分。其他的都不要。,。。 
  代码: 

<iframe name=123 align=middle marginwidth=0 marginheight=0 vspace=-170 hspace=0 src="http://www.jb51.net/" frameborder=no scrolling=no width=776 height=2500></iframe>

       其实相当于把源页面截图在iframe中显示,控制插入页被框架覆盖的深度 marginwidth=0 marginheight=0;控制框架覆盖上部分的深度 vspace=-170 
  scrolling滚动条要否(auto、yes、no) frameborder框架的边框大小,width=776 height=2500此框架的大小。 
    如果把frameborder设为1,效果就像文本框一样 

  透明的IFRAME的用法 
  必需IE5.5以上版本才支持 
  在transparentBody.htm文件的<body>标签中,我已经加入了style=”background-color=transparent” 通过以下四种IFRAME的写法我想大概你对iframe背景透明效果的实现方法应该会有个清晰的了解: 

<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>

 
重点1:利用javascript指定iframe的src并重新加载该iframe(见本文最下面我的项目) 
难点1:设置iframe的背景色 
a.html 中

<script> 
function setBG(){ 
var strColor=document.bgColor; 
frm.document.bgColor=strColor; 
} 
</script> 
<body style='background-color:red' οnlοad='setBG()'> 
<iframe src='about:blank' name=frm></iframe>


难点2: 
窗口与浮动帧之间的相互控制 
在脚本语言与对象层次中,包含Iframe的窗口我们称之为父窗体,而浮动帧则称为子窗体,弄清这两者的关系很重要,因为要在父窗体中访问子窗体或相反都必须清楚对象层次,才能通过程序来访问并控制窗体。 
  1、在父窗体中访问并控制子窗体中的对象 
  在父窗体中,Iframe即子窗体是document对象的一个子对象,可以直接在脚本中访问子窗体中的对象。 
  现在就有一个问题,即,我们怎样来控制这个Iframe,这里需要讲一下Iframe对象。当我们给这个标记设置了ID 属性后,就可通过文档对象模型DOM对Iframe所含的HTML进行一系列控制。 
  比如在example.htm里嵌入page.htm文件,并控制page.htm里一些标记对象:  

<Iframe src="test.htm" id="test" width="250" height="200" scrolling="no" frameborder="0"></iframe>

page.htm文件代码为: 
  

<html> 
   <body> 
    <h1 id="myH1">hello,my boy</h1> 
   </body> 
  </html>


  如我们要改变ID号为myH1的H1标记里的文字为hello,my dear,则可用: 
  document.myH1.innerText=”hello,my dear”(其中,document可省) 
  在example.htm文件中,Iframe标记对象所指的子窗体与一般的DHTML对象模型一致,对对象访问控制方式一样,就不再赘述。 

  2、在子窗体中访问并控制父窗体中对象 
  在子窗体中我们可以通过其parent即父(双亲)对象来访问父窗口中的对象。 
  如example.htm: 
  

<html> 
   <body οnclick="alert(tt.myH1.innerHTML)"> 
    <Iframe name="tt" src="frame1.htm" width="250" height="200" scrolling="no" frameborder="0"></iframe> 
    <h1 id="myH2">hello,my wife</h1> 
   </body> 
  </html>


  如果要在frame1.htm中访问ID号为myH2中的标题文字并将之改为”hello,my friend”,我们就可以这样写: 
 

 parent.myH2.innerText="hello,my friend"


        或者

parent.document.getElementById("myH2").innerText="hello,my friend"


  这里parent对象就代表当前窗体(example.htm所在窗体),要在子窗体中访问父窗体中的对象,无一例外都通过parent对象来进行。 

3:frame的一个子元素访问frame的另一个子元素 
例如:框架文件frame.html中嵌入了另外两个html文件 

<div styleClass="basewnd"> 
<!-- 搜索 --> 
<div id="search" name="test" align="center" class="top_list_home"> 
<iframe id="frameSearch" name="search" src="Search.html" frameBorder="0" scrolling="no" width="195px" height="150px" marginheight="0" marginwidth="0"></iframe> 
</div> 
<!-- 单位目录树 --> 
<div align="center" class="welcome_tag_home"> 
<iframe src="DirectoryTree.html" frameBorder="0" scrolling="no" width="195px" height="427px" marginheight="0" marginwidth="0"></iframe> 
</div> 
</div>


那么现在要在DirectoryTree.html文件中访问Search.html文件中的一个id为section的<font></font>标签的innerHTML属性,则可以这样: 

alert(parent.document.search.section.innerHTML)


其中search是“搜索”div的id,或者: 

alert(parent.document.getElementById("search").section.innerHTML)


或者也可以这样: 

alert(parent.document.frames["frameSublist"].name)(这是直接访问iframe)

下面这段代码可以实现IFrame自适应高度,即随着页面的长度,自动适应以免除页面和IFrame同时出现滚动条。 
源代码如下 

<script type="text/javascript"> 
//** iframe自动适应页面 **// 
//输入你希望根据页面高度自动调整高度的iframe的名称的列表 
//用逗号把每个iframe的ID分隔. 例如: ["myframe1", "myframe2"],可以只有一个窗体,则不用逗号。 
//定义iframe的ID 
var iframeids=["test"] 
//如果用户的浏览器不支持iframe是否将iframe隐藏 yes 表示隐藏,no表示不隐藏 
var iframehide="yes" 
function dyniframesize() 
{ 
var dyniframe=new Array() 
for (i=0; i<iframeids.length; i++) 
{ 
if (document.getElementById) 
{ 
//自动调整iframe高度 
dyniframe[dyniframe.length] = document.getElementById(iframeids); 
if (dyniframe && !window.opera) 
{ 
dyniframe.style.display="block" 
if (dyniframe.contentDocument && dyniframe.contentDocument.body.offsetHeight) //如果用户的浏览器是NetScape 
dyniframe.height = dyniframe.contentDocument.body.offsetHeight; 
else if (dyniframe.Document && dyniframe.Document.body.scrollHeight) //如果用户的浏览器是IE 
dyniframe.height = dyniframe.Document.body.scrollHeight; 
} 
} 
//根据设定的参数来处理不支持iframe的浏览器的显示问题 
if ((document.all || document.getElementById) && iframehide=="no") 
{ 
var tempobj=document.all? document.all[iframeids] : document.getElementById(iframeids) 
tempobj.style.display="block" 
} 
} 
} 
if (window.addEventListener) 
window.addEventListener("load", dyniframesize, false) 
else if (window.attachEvent) 
window.attachEvent("onload", dyniframesize) 
else 
window.οnlοad=dyniframesize 
</script>


以下是本人自己在实际项目开发时利用frame写的一段源代码,仅供参考: 
主文件(框架): 

<html> 
<head> 
<title>教育局资源管理系统</title> 
<script src="resources/js/DirectoryTree/DirectoryTree.js"></script> 
<script src="resources/js/date.js"></script> 
<link rel="stylesheet" type="text/css" href="resources/css/frame.css"> 
<link rel="stylesheet" type="text/css" href="resources/css/global.css"> 
<script language="javascript"> 
function setBgColor() 
{ 
var bg=document.bgColor 
bottom.document.bgColor=bg 
} 
</script> 
</head> 
<body bgcolor="#f9edff" οnlοad="setBgColor()"> 
<div styleClass="basewnd"> 
<!-- LOGO --> 
<div align="center" class="flag"> 
<iframe src="resources/HTMLFolders/Logo.html" frameBorder="0" scrolling="no" width="950px" height="115px" marginheight="0"></iframe> 
</div> 
<!-- 用户登陆 --> 
<div align="center" class="user"> 
<iframe src="resources/HTMLFolders/UserLogin.html" frameBorder="0" scrolling="no" width="195px" height="150px" marginheight="0" marginwidth="0"></iframe> 
</div> 
<!-- 搜索 --> 
<div id="search" name="test" align="center" class="top_list_home"> 
<iframe id="frameSearch" name="search" src="resources/HTMLFolders/Search.html" frameBorder="0" scrolling="no" width="195px" height="150px" marginheight="0" marginwidth="0"></iframe> 
</div> 
<!-- 导航条 --> 
<div align="center" class="navigation"> 
<iframe src="resources/HTMLFolders/Navigation.html" frameBorder="0" scrolling="no" width="950px" height="25px" marginheight="0" marginwidth="0"></iframe> 
</div> 
<!-- 最新主题列表 --> 
<div align="center" class="newest_topic"> 
<iframe src="resources/HTMLFolders/Sublist.html" frameBorder="0" scrolling="no" width="540px" height="427px" marginheight="0" marginwidth="0"></iframe> 
</div> 
<!-- 单位目录树 --> 
<div align="center" class="welcome_tag_home"> 
<iframe src="resources/HTMLFolders/DirectoryTree.html" frameBorder="0" scrolling="no" width="195px" height="427px" marginheight="0" marginwidth="0"></iframe> 
</div> 
<!-- 页尾 --> 
<div align="center" class="rights_home"> 
<iframe id="bottom" name="bottom" src="resources/HTMLFolders/Bottom.html" frameBorder="0" scrolling="no" width="950px" height="100px" marginheight="0" marginwidth="0" allowTransparency="true" style="background-color: red"></iframe> 
</div> 
</div> 
</body> 
</html>


被引用的文件UserLogin.html: 

<link rel="stylesheet" type="text/css" href="../css/global.css"> 
<table border="0" align="left" width="193" cellpadding="1" cellspacing="0" style="border-style:solid;border-width:1px;border-color:#eeeeee;"> 
<tr><td> 
<table border="0" align="left" width="190" cellpadding="0" cellspacing="0"> 
<tr> 
<td align="left" valign="middle" width="20" height="25" class="tdfnt12px" background="../images/title_bar2.png" > 
</td> 
<td align="left" valign="bottom" height="25" class="tdfnt12px" background="../images/title_bar2.png"> 
<font style="height:18px;font-family:宋体;font-size:14px;">&nbsp;<b>会员登录</b></font> 
</td> 
</tr> 
</table> 
</td></tr> 
</table> 
<div id="divLogin" style="visibility:visible;position:absolute;left:10px;top:30px"> 
<table border="0" align="left" width="193" cellpadding="1" cellspacing="0" style="border-style:solid;border-width:0px;border-color:#eeeeee;"> 
<tr> 
<td align="left" valign="bottom" height="45"><font class="normal">用户名:</font> 
<td valign="bottom"><input type="text" name="userAreaUserName" id="userAreaUserName" class="id" maxlength="16"/></td> 
</tr> 
<tr> 
<td align="left" height="40"><font class="normal">密码:</font> 
<td><input type="password" name="userAreaUserPwd" id="userAreaUserPwd" class="pwd" maxlength="16"/></td> 
</tr> 
<tr> 
<td align="center" colspan="2" class="tdfnt12px"> 
<input type="submit" value="登录" style="color:black;border-color:skyblue;border-style:solid;border-width:0px;vertical-align:middle;font-family:宋体;width:68px;height:24px;background:url(resources/images/ButtonBg02.png);"/> 
</td> 
</tr> 
</table> 
</div>


现在假设“最新主题列表”文件中有一个超链接,点击其,包含“最新主题列表”的iframe就重新加载,此时需要利用javascript来实现: 

<a href="" οnclick="redirect(); return false">www.baidu.com</a> 
<script language="javascript"> 
function redirect() 
{ 
parent.document.frames["frameSublist"].location.href="www.baidu.com" 
} 
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值