访问iframe里面内容的方法(解决兼容性问题)

文章摘抄至 http://www.csharpwin.com/dotnetspace/3774r938.shtml

 

假如你当前主页面中嵌入了一个iframe,ID为:iframeid (很多网页内部弹出窗口也会使用iframe);

iframe里面定义了一个javascript函数,函数名为:myFunc;

需要在主页面调用iframe的这个函数,请参考:

 

1. 首先这么调用在任何浏览器下都不行,报告找不到函数的异常:

document.getElementById('iframeid').myFunc(); 

 

2.有人说,这么调用:

document.frames["iframeid"].myFunc(); 

在IE浏览器下面确实可以,但是在Firefox中没有document.frames对象,所以也不行

 

3.兼容IE和Firefox的调用方法

document.getElementById('iframeid').contentWindow.myFunc();

 

4. 根据html标签访问里面的数据

4.1 根据javascrtip原始代码查询html标签内容

document.getElementById('test_iframe').contentWindow.document.getElementsByTagName('pre')[0].innerHTML)

4.2使用jquery根据html标签查找内容

$("pre",document.getElementById('test_iframe').contentWindow.document).attr("innerHTML")

 

IE6、IE7、FF3 测试都通过

你可能感兴趣的:(iframe)