Iframe父子间元素操作
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 必须在iframe加载完后才有效
=================================
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[]框架集数组方法 -- 中括号中是iframe的name值
- document.getElementById('iframe1').onload=function(){
- frames["iframe1"].run();
- }
===================================
4、在iframe中访问父级窗体的方法和属性 //window 可以不写
- //这里不用写contentWindow 写了之后会报错
- 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的事件,
主流浏览器都支持 iframe.onload=function....
在ie下需要用attachEvent绑定事件
7、在iframe所引入的网址写入防钓鱼代码
if(window!=window.top){
window.top.location.href=window.location.href;
}
8、获取iframe的高度
iframe.contentWindow.document.body.offsetHeight;
- //这里不用写contentWindow 写了之后会报错
- window.parent.attributeName; // 访问属性attributeName是在父级窗口的属性名
- window.parent.Func(); // 访问属性Func()是在父级窗口的方法
- $('#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所引入的网址写入防钓鱼代码
if(window!=window.top){
window.top.location.href=window.location.href;
}
8、获取iframe的高度
iframe.contentWindow.document.body.offsetHeight;
window.top.location.href=window.location.href;
}
Iframe父子间元素操作的更多相关文章
- iframe兄弟间和iframe父子间的值传递问题
在网上查了资料.iframe的参数传递问题.有很多答案都是不可行的.现在将收集的资料整理一下.已经验证通过.以下如有问题请及时指正. 1. iframe兄弟之间值传递 举例说明:index页面中有两个 ...
- JS方法在iframe父子窗口间的调用
本文向大家简单介绍一下iframe父子窗口间JS方法调用,JavaScript 被数百万计的网页用来改进设计.验证表单.检测浏览器.创建cookies,以及更多的应用,希望本文介绍对你有所帮助. if ...
- iframe父子操作
1.js在iframe子页面操作父页面元素代码: window.parent.document.getElementByIdx_x("父页面元素id"); 2.js在父页面获取if ...
- jquery/js iframe 元素操作
1.判断id/ class 是否存在? <script> $(function(){ if(("#id_name").length()>0){ //如果id 存在 ...
- 函数bsxfun,两个数组间元素逐个计算的二值操作
转自http://www.cnblogs.com/rong86/p/3559616.html 函数功能:两个数组间元素逐个计算的二值操作 使用方法:C=bsxfun(fun,A,B) 两个数组A合B间 ...
- Java Selenium (十二) 操作弹出窗口 & 智能等待页面加载完成 & 处理 Iframe 中的元素
一.操作弹出窗口 原理 在代码里, 通过 Set<String> allWindowsId = driver.getWindowHandles(); 来获取到所有弹出浏览器的句柄, 然 ...
- 使用jquery操作iframe中的元素
使用jquery操作iframe中的元素<iframe src="/test/demo.htm" width="99%" height="300 ...
- jquery iframe父子框架中的元素访问方法
在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素 1. 格式:window. ...
- iframe父子元素获取
jquery.js调用iframe父窗口与子窗口元素的方法 1. jquery在iframe子页面获取父页面元素代码如下: $("#objid",parent.document) ...
随机推荐
- 才知道 Windows Live Writer Source Code plugin for SyntaxHighlighter 更新到2.0了
这是我用 Windows Live Writer 发布的第一篇文章! 在官方网站看到 Windows Live Writer Source Code plugin for SyntaxHighligh ...
- oracle 容灾库日常维护 ,健康检查脚本 以及常见问题分析
select DEST_ID, APPLIED_SCN FROM v$archive_dest select * from v$dataguard_status; SELECT gvi.thread# ...
- Spark Mllib里的如何对单个数据集用斯皮尔曼计算相关系数
不多说,直接上干货! import org.apache.spark.mllib.stat.Statistics 具体,见 Spark Mllib机器学习实战的第4章 Mllib基本数据类型和Mlli ...
- C# 读写XML文件的方法
C# 读写XML文件的方法 一.写XML文件 XmlDocument xmlDocument = new XmlDocument();xmlDocument.AppendChild(xmlDocume ...
- CentOS6.x之emacs安装配置编译
刚开始学习linux,干学没什么意思,想在linux下写写程序,了解到linux下使用较多的是emacs和vim,在youtobe上分别看了看这两个工具进行开发的视频,个人感觉emacs比较酷一点,所 ...
- android 开发-数据存储之共享参数
android提供5中数据存储方式 数据存储之共享参数 内部存储 扩展存储 数据库存储 网络存储 而共享存储提供一种可以让用户存储保存一些持久化键值对在文件中,以供其他应用对这些共享参数进行调用.共 ...
- web 单例 多例
单例多例需要搞明白两个问题:1. 什么是单例多例:2. 如何产生单例多例:3. 为什么要用单例多例4. 什么时候用单例,什么时候用多例:1. 什么是单例多例:所谓单例就是所有的请求都用一个对象来处理, ...
- Servlet高级部分Listener
监听器的使用场景: ①:统计在线人数 ②:实现单一登录[一个账号只能在一台机器上登录] Servlet中的8大监听器: 1. ServletContextListener [接口方 ...
- 使用random函数实现randint函数的功能
首先说明一下 random函数是random模块中的一个函数 首先要导入random模块 import random random函数的功能 #生成某一范围(0-1)内的随机小数print(rando ...
- windows下 php集成环境如何通过cmd执行命令
---恢复内容开始--- php学习过程中 Windows环境下的php集成程序很多 这样方便了学习 但是在熟悉命令使用方面可以说是十分不便 本文将从两个方便 向大家介绍如何快速通过cmd命令实现命令 ...