87,907
社区成员
发帖
与我相关
我的任务
分享
<html>
<head>
<title>differences</title>
</head>
<body>
<iframe id="myframe" src="http://www.baidu.com" height=400 width=400></iframe>
</body>
<script type="text/javascript">
document.body.onload = function(){
//这种写法没问题
//document.getElementById('myframe').src = 'http://www.google.com';
//这种写法会出错
//document.getElementById('myframe').location.href='http://www.google.com';
//这种写法可行
//window.frames[0].location.href='http://www.google.com';
//这种会有permission denied错误。
//window.frames[0].src='http://www.google.com';
}
</script>
</html>
<iframe id="content" name="content" width="100%" height="100%" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" onload="this.document?this.style.height=content.document.body.scrollHeight+5:this.height=this.contentDocument.body.scrollHeight+5;">
</iframe>
this.style.height=content.document.body.scrollHeight+5 //支持IE
this.height=this.contentDocument.body.scrollHeight+5 //支持FF
其实主要你搞清楚 你要操作的是iframe对象本身(比如src是iframe的属性)
还是iframe对象里面的DOM元素就可以了(比如location是iframe里面DOM的window的属性)
var obj = document.getElementById(“iframe”);//获取对象
var dom = document.all.frames[“iframe”];//获取DOM
然后就卡这两条.一个是对象 一个是DOM.
======================
最后记住这两个语法格式 相信操作就没问题了
document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:
在IE为是frames[id].document或document.getElementById(id).contentWindow.document;
在firefox为frames[name].contentDocument或document.getElementById(id).contentDocument;
var obj = document.getElementById(“iframe”);//获取对象
3.var dom = document.all.frames[“iframe”];//获取DOM
如果只想改变iframe的 src 或者 border ,scrolling 等attributes(与property不是一个概念
,property是不能写在标签内的,比如:scrollHeight,nnerHTML等),就需要用到第一种方法。
如果想取得iframe的页面(不是iframe本身)。就需要使用第二种方法,因为它取得的是一个完整的DOM模型,
比如想得到iframe的document.body的内容,就只能用第二种方法
另:
document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:
在IE为是frames[id].document或document.getElementById(id).contentWindow.document;
在firefox为frames[name].contentDocument或document.getElementById(id).contentDocument;
如果只想改变iframe的 src 或者 border ,scrolling 等attributes(与property不是一个概念,property是不能写在标签内的,比如:scrollHeight,nnerHTML等),就需要用到第一种方法。
如果想取得iframe的页面(不是iframe本身)。就需要使用第二种方法,因为它取得的是一个完整的DOM模型,比如想得到iframe的document.body的内容,就只能用第二种方法
另:
document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:
在IE为是frames[id].document或document.getElementById(id).contentWindow.document;
在firefox为frames[name].contentDocument或document.getElementById(id).contentDocument;