html的iframe用法

2016-02-19 22:21 93 1 收藏

今天图老师小编要向大家分享个html的iframe用法教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - Web开发 】

    好多同志对 iframe 是如何控制的,并不是十分了解,基本上还处于一个模糊的认识状态.

  注意两个事项,ifr 是一个以存在的 iframe 的 ID 和 NAME 值:

    document.getElementById(“ifr”);
    window.frames[“ifr”];

  要想使用iframe内的函数,变量就必须通过第二种方法.因为它取的是一个完整的DOM模型(不知道这样说对不对).第一种方法只是取出了一个OBJECT而已.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  如果只想改变iframe的 src 或者 border , scrolling 等 attributes(与property不是一个概念,property是不能写在标签内的,比如:scrollHeight,innerHTML等),就需要用到第一种方法.

  如果想取得iframe的页面(不是iframe本身),就需要使用第二种方法,因为它取得的是一个完整的DOM模型,比如想得到iframe的document.body的内容,就只能用第二种方法.

  还要注意的是,如果在iframe的页面未完全装入的时候,调用iframe的DOM模型,会发生很严重的错误,所以,你要准备一个容错模式.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  下面是示例,一个是aa.htm,一个是bb.htm,请先考到本地在运行,原因不用我在说了吧!本文来自http://bianceng.cn(编程入门)

  aa.htm

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
html xmlns="http://www.w3.org/1999/xhtml"
head
   titleUntitled Page/title
style type="text/css"
!--
body{
margin:0px;
}
--
/style
/head
body
iframe id="ifr" name="ifr" width="100%" height="500" src="bb.htm"/iframe
/body
/html
script language="javascript" type="text/javascript"
var aa_value="I'm a variant in Top window!";
var ifr_id=document.getElementById("ifr");
var ifr_window=window.frames["ifr"];
alert("Alert from Top window : Can't get iframe's variant by ifr_id, it will return :" + ifr_id.bb_var);
alert("Alert from Top window : Can't get iframe's DOM model by ifr_id ,it will return :" + ifr_id.window);
alert("Alert from Top window : Get src from id :" + ifr_id.src);
alert("Alert from Top window : Get href from window:" + ifr_window.document.location.href);
//由于bb.htm可能还未装载完成,所以,下冇的可能引发错误
//调用iframe内的函数 ifr_window.bb();
//调用iframe内的变量
alert("Alert from Top window : " + ifr_window.bb_var);
//
alert("Alert from Top Window :" + ifr_window.document.body.innerHTML);
function aa(msg){
   alert("I'm alerting from Top window ,and I received a msg: " + msg);
}
/script

  bb.htm

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
html xmlns="http://www.w3.org/1999/xhtml"
head
   titlesub frame/title
style type="text/css"
!--
html,body{
margin:0px;
width:90%;
}
--
/style
/head
body
   I'm a sub frame!
   br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...br /
   ...
/body
/html
script language="javascript" type="text/javascript"
var bb_var="I'm a variable in ifr";
function bb(){
   alert("Alert from iframe :I'm frame ifr's function")
}
//获取父页面的变量
alert("Alert from iframe parent.ifr_id::" + parent.ifr_id);
alert("Alert from iframe parent.aa_value : " + parent.aa_value);
//通过父页面的ifr_id来改变 iframe的高度
alert("Alert from iframe : ifr's clientHeight :" +document.body.clientHeight);
parent.ifr_id.height=document.body.clientHeight;
alert("Alert from iframe : ifr's scrollHeight : " + document.body.scrollHeight);
//调用父窗体的函数:
parent.aa("I will calling a function which is Top window's ");
//改变父窗体的标题:
alert("Alert from iframe : I will changing Top window's title");
top.document.title="The title value changed";
//通过父窗体的ifr_id来改变的border 与scrolling
alert("Alert from iframe : I will change my border and scrolling :");
top.ifr_id.border=0;
top.ifr_id.scrolling="no";
/script

来源:https://www.tulaoshi.com/n/20160219/1628216.html

延伸阅读
标签: Web开发
iframe src="1.htm" name="ifrmname" id="ifrmid"/iframe 方案一:用iframe的name属性定位 input type="button" name="Button" value="Button"onclick="document.frames('ifrmname').location.reload()" 或 input type="button" name=&quo...
标签: Web开发
在as3中使用特殊字符的时候,不小心就会出现错误,要么字符没有被显示,要么显示的不对,本文章就此做一个简单的分析,如有更好的分析,请一定要跟贴。 sample.xml和sample_cdata.xml是本例需要用到的xml文件,请点击查看他们的内容,本例以特殊字符 和 为例;文本字符串变量为txt, 显示文本的名称为shwo_txt。 由于html和xml中和...
标签: Web开发
本文详细介绍了iframe框架高度随内容的变化而变化的代码。 iframe src="XXX.htm" width="700px" frameborder="0" id="frmsrc" scrolling="no" marginheight="0" onload="AutoIframe()"/iframe script type="text/javascript"     functi...
标签: Web开发
ZZzzz~ may wind Iframe src="/URL" width="x" height="x" scrolling="[OPTION]" frameborder="x"/iframe src:文件的路径,既可是HTML文件,也可以是文本、ASP等; width、height:"画中画"区域的宽与高; scrolling:当SRC的指定的HTML文件在指定的区域不显不完时,滚动选项,如果设置为NO,则不出现滚动条;如...
标签: Web开发
htmlhead meta http-equiv="Content-Type" content="text/html; charset=gb2312" meta name="GENERATOR" content="Microsoft FrontPage 4.0" meta name="ProgId" content="FrontPage.Editor.Document" titleNew Page 1/title /head body IFRAME id="test" name="...

经验教程

645

收藏

25
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部