我想在js中动态更改iframe中的页面,用原以为理所当然的方法去实现,结果几种方法却大相径庭,不得其解,请高手指点:页面(Main.aspx)中有一个iframe:
<iframe id="mainFrame" frameborder="0" width="100%" height="554px;" scrolling="auto" src='MyDesktop.aspx'></iframe>Main.aspx中引用了一个外部js文件。
1、我先用如下方法:
document.getElementById("mainFrame").src = ...;
结果ok2、然后用如下方法:
document.getElementById("mainFrame").document.location.href = ...;
结果出错
调试:alert(document.getElementById("mainFrame").document.location.href),没有出现我所期望的MyDesktop.aspx,而是Main.aspx!3、最后用如下方法:
var mainFrame = top["mainFrame"];
mainFrame.document.location.href = ...;
结果ok
调试:alert(mainFrame.document.location.href),得到的是MyDesktop.aspx疑问:在方法2和方法3中,用document.getElementById("mainFrame")和用top["mainFrame"]得到的对象为什么不一样???
<iframe id="mainFrame" frameborder="0" width="100%" height="554px;" scrolling="auto" src='MyDesktop.aspx'></iframe>Main.aspx中引用了一个外部js文件。
1、我先用如下方法:
document.getElementById("mainFrame").src = ...;
结果ok2、然后用如下方法:
document.getElementById("mainFrame").document.location.href = ...;
结果出错
调试:alert(document.getElementById("mainFrame").document.location.href),没有出现我所期望的MyDesktop.aspx,而是Main.aspx!3、最后用如下方法:
var mainFrame = top["mainFrame"];
mainFrame.document.location.href = ...;
结果ok
调试:alert(mainFrame.document.location.href),得到的是MyDesktop.aspx疑问:在方法2和方法3中,用document.getElementById("mainFrame")和用top["mainFrame"]得到的对象为什么不一样???
解决方案 »
- 求教一个JS函数继承的问题
- JS如何判断输入框(就是text或textarea)的值只是空字符串或只带空格得字符串
- 关于跨域访问的问题
- js可以控制网络上的flash吗?
- setTimeout和setInterval函数有何区别
- 改变div大小,在ie下ok,用firefox就没反应,为什么啊?
- 请问如何解决"不能为空或对象"的小问题?
- 一个函数依赖另一个函数,像拥有这种依赖关系的两个函数有没有的专业称谓?
- zhaoweiwei进!!!!!!!!!!!!!!!!!
- 这个多级菜单为什么在 IE6下面正常 在谷歌浏览器和IE8不正常了?
- 正则表达式 | ├ 轻轨 提取 轻轨
- 求jquery实现div始终垂直居中于浏览器代码?
我的疑问是:document.getElementById("mainFrame")肯定指向iframe,那么top["mainFrame"]又指向什么呢?js中top到底有何用法?
top["mainFrame"]其实获得的是iframe中的子页对象 可以获得子页的document