您的位置:首页 > Web前端 > javascript > 正文

js操作iframe

更多 时间:2015-12-24 类别:Web前端 浏览量:178

js操作iframe

js操作iframe

1、获取iframe的window对象

存在跨域访问限制。

iframeElement.contentWindow

 

2、获取iframe的document对象

存在跨域访问限制。

chrome: iframeElement.contentDocument

firefox: iframeElement.contentDocument

ie:iframeElement.contentWindow.document

 

3、iframe中获得父页面的window对象

存在跨域访问限制。

父页面:window.parent

顶层页面:window.top

 

4、父页面中获取iframe中的内容

 

var iframeObj = document.getElementById('iframeid');

a:获取iframe中body里的内容

var iframeContent = iframeObj.contentWindow.document.body.innerHTML;

 

b:获取iframe中head里的内容

var iframeContent = iframeObj.contentWindow.document.head.innerHTML;

 

c:获取iframe中html里的内容

var iframeContent = iframeObj.cententWindow.document.documentElement.innerHTML;

 

d:获取iframe中某个元素里的内容

var iframeContent = iframeObj.contentWindow.document.getElementById('element_id').innerHTML;

 

5、frames

window.frames可以取到页面中的iframe、frame等,需要注意的是取到的是window对象,返回的是一个数组。

 

标签:iframe