js获取iframe中的元素以及在iframe中获取父级的元素(包括iframe中不存在name和id的情况)
2015-11-05 16:27
726 查看
第一种情况:iframe中不存在name和id的方法:(通过contentWindow获取)
var iframe = document.getElementsByTagName('iframe')[0];
var ifr_document = iframe.contentWindow.document;//iframe中的文档内容
或者:
var _iframe = document.getElementByIdx_x('iframeId').contentWindow;
var _div =_iframe.document.getElementByIdx_x('objId');
或者:
var frameWin=document.getElementById('iframe').contentWindow; //window对象
var frameDoc=document.getElementById('iframeId').contentWindow.document //document对象
var frameBody=document.getElementById('iframeId').contentWindow.document.body //body对
第二种情况:iframe中存在name或者id的方法:(通过frames[]数组获取)
document.frames['iframe的name'].document.getElementById('元素的ID');
第三种情况:在iframe中获取父级页面的id元素 :
var obj=window.parent.document.getElementById('objId') ;
$(window.parent.document).find("#objId").css('height':'height); // window可省略不写 jquery
第四种情况:父级窗体访问iframe中的属性
a、 用contentWindow方法
document.getElementById('iframe1').onload=function(){
this.contentWindow.run();
}
b、用iframes[]框架集数组方法
document.getElementById('iframe1').onload=function(){
frames["iframe1"].run();
}
第五种情况:在iframe中访问父级窗体的方法和属性 //window 可以不写
window.parent.attributeName; // 访问属性attributeName是在父级窗口的属性名
window.parent.Func(); // 访问属性Func()是在父级窗口的方法
第五种情况:让iframe自适应高度
$('#iframeId').load(function() { //方法1
var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);
var h=$(this).contents().height();
$(this).height(h+'px');
});
$('#iframeId').load(function() { //方法2
var iframeHeight=$(this).contents().height();
$(this).height(iframeHeight+'px');
});
var iframe = document.getElementsByTagName('iframe')[0];
var ifr_document = iframe.contentWindow.document;//iframe中的文档内容
或者:
var _iframe = document.getElementByIdx_x('iframeId').contentWindow;
var _div =_iframe.document.getElementByIdx_x('objId');
或者:
var frameWin=document.getElementById('iframe').contentWindow; //window对象
var frameDoc=document.getElementById('iframeId').contentWindow.document //document对象
var frameBody=document.getElementById('iframeId').contentWindow.document.body //body对
第二种情况:iframe中存在name或者id的方法:(通过frames[]数组获取)
document.frames['iframe的name'].document.getElementById('元素的ID');
第三种情况:在iframe中获取父级页面的id元素 :
var obj=window.parent.document.getElementById('objId') ;
$(window.parent.document).find("#objId").css('height':'height); // window可省略不写 jquery
第四种情况:父级窗体访问iframe中的属性
a、 用contentWindow方法
document.getElementById('iframe1').onload=function(){
this.contentWindow.run();
}
b、用iframes[]框架集数组方法
document.getElementById('iframe1').onload=function(){
frames["iframe1"].run();
}
第五种情况:在iframe中访问父级窗体的方法和属性 //window 可以不写
window.parent.attributeName; // 访问属性attributeName是在父级窗口的属性名
window.parent.Func(); // 访问属性Func()是在父级窗口的方法
第五种情况:让iframe自适应高度
$('#iframeId').load(function() { //方法1
var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);
var h=$(this).contents().height();
$(this).height(h+'px');
});
$('#iframeId').load(function() { //方法2
var iframeHeight=$(this).contents().height();
$(this).height(iframeHeight+'px');
});
相关文章推荐
- JSP EL简介
- js 倒计时
- AnjularJS初学(AnjularJS跨域取值)
- 把一些表单属性封装一个JSON
- js键盘码
- AnjularJS初学(AnjularJS验证)
- js识别浏览器
- JavaScript高级程序设计之DOM 扩展之专有扩展之滚动第11.4.5讲
- 彻底理解JavaScript原型
- extjs操作组件的形式
- AnjularJS初学(模拟输入限制)
- JS将秒转化成时分秒
- python 抓取javascript 动态数据
- Javascript模块化编程(一):模块的写法
- 有用的js类库
- js原生之一个面向对象的应用
- JavaScript高级程序设计之DOM 扩展之专有扩展之插入文本第11.4.4讲
- js原生之函数
- Dynamics CRM2011 在Visual Studio中开启Javascript的Xrm.Page智能提示
- Dynamics CRM2011 在Visual Studio中开启Javascript的Xrm.Page智能提示