js获取iframe和父级之间元素,方法、属,获取iframe的高度自适应iframe高度
摘自:http://blog.csdn.net/kongjiea/article/details/38870399
1、在父页面 获取iframe子页面的元素
(在同域的情况下 且在http://下测试,且最好在iframe onload加载完毕后 dosomething...)
js写法
a、同过contentWindow获取
也有用contentDocument 获取的 但是contentWindow 兼容各个浏览器,可取得子窗口的 window 对象。
contentDocument Firefox 支持,> ie8 的ie支持。可取得子窗口的 document 对象。
获取方法
- 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.contentDocument 方法 //但是ie 6,7不支持
b、通过frames[]数组获取
(但是必须在ifram框架加载完毕后获取,iframe1是iframe的name属性)
- document.getElementById('iframId').onload=function(){
- var html= window.frames["name属性"].document.getElementById('iframe中的元素的id').innerHTML;
- alert(html)
- }
- 如果要获取iframe中的iframe
- document.getElementById('iframId').onload=function(){
- var html= window.frames["name属性"].frames["name属性"].document.getElementById('iframe中的元素的id').innerHTML;
- alert(html)
- }
jq写法:必须在iframe加载完毕以后才有效
- a、$("#iframe的ID").contents().find("#iframe中的控件ID").click();//jquery 方法1 必须在iframe加载完后才有效
- b、$("#iframe中的控件ID",document.frames("frame的name").document)//方法2 <span style="font-family: Arial, Helvetica, sans-serif;">必须在iframe加载完后才有效</span>
=================================
2、在iframe中获取父级页面的id元素
(在同域的情况下且在http://下测试,最好是 iframe记载完毕再dosomething)
js写法:
获取父级中的objid
- var obj=window.parent.document.getElementById('objId')
window.top 方法可以获取父级的父级的....最顶层的元素对象
jq写法:
- $('#父级窗口的objId', window.parent.document).css('height':'height); // window可省略不写
- 或者
- $(window.parent.document).find("#objId").css('height':'height); // window可省略不写
===================================
3、父级窗体访问iframe中的属性
(经测试,在ie中最好用原生的onload事件,如果用jq的load把iframe加载完毕 有时候方法调用不到 多刷新才有效果)
- a、 用contentWindow方法
- document.getElementById('iframe1').onload=function(){
- this.contentWindow.run();
- }
- b、用iframes[]框架集数组方法
- document.getElementById('iframe1').onload=function(){
- frames["iframe1"].run();
- }
===================================
4、在iframe中访问父级窗体的方法和属性 //window 可以不写
- window.parent.attributeName; // 访问属性attributeName是在父级窗口的属性名
- window.parent.Func(); // 访问属性Func()是在父级窗口的方法
5、让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');
- });
6、iframe的onload的事件,
7、在iframe所引入的网址写入防钓鱼代码
window.top.location.href=window.location.href;
}
8、获取iframe的高度
js获取iframe和父级之间元素,方法、属,获取iframe的高度自适应iframe高度的更多相关文章
- js获取iframe中的元素以及在iframe中获取父级的元素(包括iframe中不存在name和id的情况)
第一种情况:iframe中不存在name和id的方法:(通过contentWindow获取) var iframe = document.getElementsByTagName('iframe' ...
- iframe里访问父级里的方法属性
window.parent.attributeName; // 访问属性attributeName是在父级窗口的属性名 window.parent.Func(); // 访问属性Func()是在父 ...
- Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法
在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素 1. 格式:window ...
- 在iframe窗体内 获取父级的元素;;在父窗口中获取iframe中的元素
在iframe中获取父窗口的元素 $(‘#父窗口中的元素ID’, parent.document).click(); 在父窗口中获取iframe中的元素 $(“#iframe的ID”).content ...
- Js跨域、父级窗口执行JS赋值、取值,更改元素
网站域名: A:http://www.xxoo.com/a.html B:http://www.aabb.com/b.html B网站嵌套与A网站(A的a中的Iframe指向B中的b)b要让父级a页面 ...
- 在iframe的父级作用域操作,ifame中的元素。。
frames["iframe的name"].SchDatas SchDatas为方法名js中 frames["iframe的name"].document.ge ...
- dirname的用法:获取文件的父级目录路径
命令:dirname 获取文件的路径(到父级目录)用法:dirname file_name [root@bogon opt]# a=$(dirname /mnt/a/b/c/d/a.sh) [root ...
- javascript父级鼠标移入移出事件中的子集影响父级的处理方法
一.我们先分析其产生的原因: 1.当鼠标从父级移入子集时触发了父级的两个事件:a.父级的mouseout事件(父级离开到子集):b.由于事件冒泡影响,又触发了父级的mouseover事件(父级移入父级 ...
- Easyui弹出窗体在iframe的父级页面显示
今天做EasyUI学习的预到了一个这样的问题:通过iframe加载的一个页面在调用$.messager.alert();这个方法后只能在iframe中显示alert效果而不是在全局的页面上显示这并不我 ...
随机推荐
- html 简单的预缓存
切图生成html,加鼠标响应,预缓存 <style> .d4{ width:190; height:170; background-image: url(images/未标题-1_09-1 ...
- (转)git使用教程
git基础使用:http://geek.csdn.net/news/detail/77455 github介绍:http://stormzhang.com/github/2016/05/25/lear ...
- FreeRTOS系列第17篇---FreeRTOS队列
本文介绍队列的基本知识,具体源代码分析见<FreeRTOS高级篇5---FreeRTOS队列分析> 1.FreeRTOS队列 队列是基本的任务间通讯方式.能够在任务与任务间.中断和任务间传 ...
- js json ie不支持json
JSON是包含在JScript 5.8中,而为了向下兼容ie8只有在文档模式是”Internet Explorer 8 Standards”的时候才使用JScripte 5.8,其他时候使用JScri ...
- 用python解析html--SGMLParser
sgmllib.py 包含一个重要的类: SGMLParser.SGMLParser 将 HTML 分解成有用的片段, 比如开始标记和结束标记.一旦它成功地分解出某个数据为一个有用的片段,它会根据 所 ...
- ChemDraw Pro和ChemBio3D Ultra有什么区别
作为ChemOffice 14的两个重要组件,ChemDraw Pro和ChemBio3D Ultra都有着自己的特点,ChemDraw Pro以绘制平面结构而ChemBio3D Ultra以绘制立体 ...
- hdu 3681(bfs+二分+状压dp判断)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...
- 面试题思考:java中快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?
一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...
- 开源的Eclipse的文件转码插件,可以在不影响中文的情况下改变项目文件编
http://www.blogjava.net/lifesting/archive/2008/04/11/192250.html, 感谢此作者! 问题描述: 我们项目开发都统一采用utf-8格式编码, ...
- activeMQ "HelloWorld"实现
本文主要介绍activeMQ在应用程序中是如何使用的,同个两个实例进行说明,这两个实例分别针对P2P模式和Pub/Sub模式. 开发环境 操作系统:Ubuntu 16.10 开发平台:Eclipse ...