摘自: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. 怎样在xilinx SDK中显示行号

    Window→preferences→editor→test editor 对ecilpse的通用方法 打开Eclipse软件,在菜单中选择窗体--首选项.打开新的窗体. 在新的窗体中依次选择常规-- ...

  2. PHP 中 json_encode中文处理、urlencode方法、post中文乱码

    当使用php自带的json_encode对数据进行编码时,中文都会变成unicode,导致不可读.如:对字符串”厦门“进行json_encode后,输出的是"\u53a6\u95e8&quo ...

  3. Effective JavaScript Item 55 接受配置对象作为函数參数

    接受配置对象作为函数參数 尽管保持函数接受的參数的顺序非常重要,可是当函数可以接受的參数达到一定数量时.也会让用户非常头疼: var alert = new Alert(100, 75, 300, 2 ...

  4. 【ask】Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

    1.周五对nodejs tcp长连接服务器框架进行压测,一切开始比较常规(没什么特殊问题). 2.突然手一哆嗦,把压测用的客户端群一起关闭了. 3.这个时候nodejs的服务器爆出了"Cau ...

  5. requestAnimationFrame 实现JS动画

    requestAnimationFrame会随着浏览器绘制窗口的频率来绘制动画以达到最好的用户体验 // let raf = (function(){ // return window.request ...

  6. Hibernate 中多对多(many-to-many)关系的查询语句

    两个对象: 学生表:Student 课程表:Course 两者的关系是多对多,当查询Student对象,并以Course对象作为条件时的sql语句写法如下: select pa from Studen ...

  7. Java版接口自动化--初稿

    一.接口参数的获取: 1.参数通过Excel读取,并将结果写入Excel中 package org.fanqi.operateExcel; import java.io.FileInputStream ...

  8. PIP源使用国内镜像

    对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题.所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装成 ...

  9. 获取当前日期和农历的js代码

    来自:http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html 获取当前日期 getToday.js: function GetCu ...

  10. 第08章—整合Spring Data JPA

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...