摘自: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 对象。

获取方法

  1. var frameWin=document.getElementById('iframe').contentWindow;    //window对象
  2. var frameDoc=document.getElementById('iframeId').contentWindow.document  //document对象
  3. var frameBody=document.getElementById('iframeId').contentWindow.document.body   //body对象

还有iframe.contentDocument 方法 //但是ie 6,7不支持

b、通过frames[]数组获取

(但是必须在ifram框架加载完毕后获取,iframe1是iframe的name属性)

  1. document.getElementById('iframId').onload=function(){
  2. var html= window.frames["name属性"].document.getElementById('iframe中的元素的id').innerHTML;
  3. alert(html)
  4. }
  5. 如果要获取iframe中的iframe
  6. document.getElementById('iframId').onload=function(){
  7. var html= window.frames["name属性"].frames["name属性"].document.getElementById('iframe中的元素的id').innerHTML;
  8. alert(html)
  9. }

jq写法:必须在iframe加载完毕以后才有效

  1. a、$("#iframe的ID").contents().find("#iframe中的控件ID").click();//jquery 方法1 必须在iframe加载完后才有效
  2. 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

  1. var obj=window.parent.document.getElementById('objId')

window.top 方法可以获取父级的父级的....最顶层的元素对象

jq写法:

  1. $('#父级窗口的objId', window.parent.document).css('height':'height);  // window可省略不写
  2. 或者
  3. $(window.parent.document).find("#objId").css('height':'height);   // window可省略不写

===================================

3、父级窗体访问iframe中的属性

(经测试,在ie中最好用原生的onload事件,如果用jq的load把iframe加载完毕 有时候方法调用不到 多刷新才有效果)

  1. a、 用contentWindow方法
  2. document.getElementById('iframe1').onload=function(){
  3. this.contentWindow.run();
  4. }
  5. b、用iframes[]框架集数组方法
  6. document.getElementById('iframe1').onload=function(){
  7. frames["iframe1"].run();
  8. }

===================================

4、在iframe中访问父级窗体的方法和属性 //window 可以不写

  1. window.parent.attributeName;  // 访问属性attributeName是在父级窗口的属性名
  2. window.parent.Func();  // 访问属性Func()是在父级窗口的方法

5、让iframe自适应高度

  1. $('#iframeId').load(function() { //方法1
  2. var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);
  3. var h=$(this).contents().height();
  4. $(this).height(h+'px');
  5. });
  6. $('#iframeId').load(function() { //方法2
  7. var iframeHeight=$(this).contents().height();
  8. $(this).height(iframeHeight+'px');
  9. });

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;

js获取iframe和父级之间元素,方法、属,获取iframe的高度自适应iframe高度的更多相关文章

  1. js获取iframe中的元素以及在iframe中获取父级的元素(包括iframe中不存在name和id的情况)

      第一种情况:iframe中不存在name和id的方法:(通过contentWindow获取) var iframe = document.getElementsByTagName('iframe' ...

  2. iframe里访问父级里的方法属性

    window.parent.attributeName;  // 访问属性attributeName是在父级窗口的属性名 window.parent.Func();  // 访问属性Func()是在父 ...

  3. Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法

    在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素  1. 格式:window ...

  4. 在iframe窗体内 获取父级的元素;;在父窗口中获取iframe中的元素

    在iframe中获取父窗口的元素 $(‘#父窗口中的元素ID’, parent.document).click(); 在父窗口中获取iframe中的元素 $(“#iframe的ID”).content ...

  5. 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页面 ...

  6. 在iframe的父级作用域操作,ifame中的元素。。

    frames["iframe的name"].SchDatas SchDatas为方法名js中 frames["iframe的name"].document.ge ...

  7. dirname的用法:获取文件的父级目录路径

    命令:dirname 获取文件的路径(到父级目录)用法:dirname file_name [root@bogon opt]# a=$(dirname /mnt/a/b/c/d/a.sh) [root ...

  8. javascript父级鼠标移入移出事件中的子集影响父级的处理方法

    一.我们先分析其产生的原因: 1.当鼠标从父级移入子集时触发了父级的两个事件:a.父级的mouseout事件(父级离开到子集):b.由于事件冒泡影响,又触发了父级的mouseover事件(父级移入父级 ...

  9. Easyui弹出窗体在iframe的父级页面显示

    今天做EasyUI学习的预到了一个这样的问题:通过iframe加载的一个页面在调用$.messager.alert();这个方法后只能在iframe中显示alert效果而不是在全局的页面上显示这并不我 ...

随机推荐

  1. php 知乎爬虫

    http://blog.jobbole.com/88788/ https://github.com/owner888/phpspider 费了半天劲安装了redis,导出cookie,发现仍是缺失很多 ...

  2. Jmeter负载测试例子

    通过浏览器操作网站在jmeter录屏控制器显示录屏例子,并且通过这例子模拟多用户(线程)来负载测试. 工具/原料   Jmeter 浏览器 1.先在测试计划创建线程组和录制Case   1 1.1 选 ...

  3. Text类型的字段进行数据替换

    一.text不大于8000 varchar和nvarchar类型是支持replace函数的,所以如果你的text不超过8000,可以先转换成前面两种类型再使用replace. UPDATE News ...

  4. javascript基础语法及使用

    前几年自学过JavaScript,由于从事安卓开发,就放弃了对js的学习,今天又捡起来重新学习了下,希望对大家有所帮助. 首先介绍下什么是JavaScript. JavaScript 是互联网上最流行 ...

  5. 【BZOJ】2019: [Usaco2009 Nov]找工作(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2019 spfa裸题.....将飞机场的费用变成负,然后spfa找正环就行了 #include < ...

  6. js常用API汇总(转)

    typeof(); 检测数据类型 String(); 转换成字符串 parseInt(); 解析出一个string或number的整数部分 parseFloat(); 解析出一个string的浮点数部 ...

  7. jQuery左右选择框

    <!DOCTYPE html> <html> <head> <title>左右选择框</title> <style type=&quo ...

  8. C# 文件读写操作整理

    http://www.cnblogs.com/wangshenhe/archive/2012/05/09/2490438.html

  9. linux jdk 6 版本下载

    http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-41940 ...

  10. linux的ssh命令

    转自:http://man.linuxde.net/ssh ssh命令 网络安全 ssh命令是openssh套件中的客户端连接工具,可以给予ssh加密协议实现安全的远程登录服务器. 语法 ssh(选项 ...