今天没事来总结一下iframe在jquery中怎么操作的,下面我来给各位介绍jquery 获取iframe子/父页面的元素及iframe在jquery高度自适应实现方法,各位朋友可参考。

jquery方法:

在iframe子页面获取父页面元素

代码如下:

 $('#objId', parent.document);

在父页面 获取iframe子页面的元素
代码如下:

 $("#objid",document.frames('iframename').document)

 $(document.getElementById('iframeId').contentWindow.document.body).html()

显示iframe中body元素的内容。

 $("#testId", document.frames("iframename").document).html();

根据iframename取得其中ID为"testId"元素。

$(window.frames["iframeName"].document).find("#testId").html()

在父窗口中操作 选中IFRAME中的所有输入框:

$(window.frames["iframeSon"].document).find(":text");

在IFRAME中操作 选中父窗口中的所有输入框:

$(window.parent.document).find(":text");

iframe框架的HTML:

<iframe src="test.html" id="iframeSon" width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>

在父窗口中操作 选中IFRAME中的所有单选钮。

$(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true");

在IFRAME中操作 选中父窗口中的所有单选钮。

$(window.parent.document).find("input[@type='radio']").attr("checked","true");

iframe框架的:

<iframe src="test.html" id="iframe1″ width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>

假设有两个页面,在相同域下.

index.html 文件内含有一个iframe:

XML/HTML代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>页面首页</title>
</head> <body>
<iframe src="iframe.html" id="koyoz" height="0" width="0"></iframe>
</body>
</html>

iframe.html 内容:

XML/HTML代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>iframe.html</title>
</head> <body>
<div id="test">www.</div>
</body>
</html>

iframe自适应高度:

$("#iPersonalInfo").load(function() {

$(this).height($(this).contents().height());

})

有一点需要注意的,我也在调试的时候才发现的,耽误了不少时间。就是绑定事件必须在iframe加载完毕之前绑定,否则不会执行。
以下是jQuery,load事件的概述
在每一个匹配元素的load事件中绑定一个处理函数。
如果绑定给window对象,则会在所有内容加载后触发,包括窗口,框架,对象和图像。如果绑定在元素上,则当元素的内容加载完毕后触发。
注意:只有当在这个元素完全加载完之前绑定load的处理函数,才会在他加载完后触发。如果之后再绑定就永远不会触发了。所以不要在$(document).ready()里绑定load事件,因为jQuery会在所有DOM加载完成后再绑定load事件。

iframe代码,注意要写ID

<iframe src="test.html" id="main" width="700" height="300" frameborder="0" scrolling="auto"></iframe>

jquery代码1:

//注意:下面的代码是放在test.html调用
$(window.parent.document).find("#main").load(function(){
var main = $(window.parent.document).find("#main");
var thisheight = $(document).height()+30;
main.height(thisheight);
});

jquery代码2:

//注意:下面的代码是放在和iframe同一个页面调用
$("#main").load(function(){
var mainheight = $(this).contents().find("body").height()+30;
$(this).height(mainheight);
});

【转】jquery iframe取得元素与自适应高度的更多相关文章

  1. jquery iframe取得元素与自适应高度

    总结一下iframe在jquery中怎么操作的,下面我来给各位介绍jquery 获取iframe子/父页面的元素及iframe在jquery高度自适应实现方法,各位朋友可参考. jquery方法: 在 ...

  2. iframe 完全跨域自适应高度

    1.跨域访问页面, 需要访问后台的页面,通过后台调转 2.跨域自适应宽高   思路:通过相互嵌套,获取跨域页面的高度,通过src传回到本域,通过parent方法设置主页的iframe的高度 index ...

  3. 如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. iframe框根据内容自适应高度

    1.页面 <iframe name="iframe_userCenter" id="iframe" frameborder=2 width=100% he ...

  5. jquery如何获取元素的滚动高度

    获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度 $(documen ...

  6. jquery iframe高度自适应

    $(document).ready(function () { $("#test").load(function () { var thisheight = $(this).con ...

  7. jquery iframe自适应高度[转]

      经典代码 iFrame 自适应高度,在IE6/IE7/IE8/Firefox/Opera/Chrome/Safari通过测试. 很古老的方法: <iframe src="../In ...

  8. jQuery实现iframe的自适应高度

    假设我们在当前页面要嵌套一个iframe 1 <iframe id="myframe" src="test.html" height="240& ...

  9. jquery 清空 iframe 的内容,,iframe自适应高度

    $(iframe).contents().find("body").html(""); iframe自适应高度 $("#AllDescription& ...

随机推荐

  1. C# 枚举(enum)

    public enum EnumStatus { On=0, Off=1 } //获取值:0 Convert.ToInt32(EnumStatus.On); //获取On EnumStatus.On; ...

  2. Android学习系列(42)--Android Studio实战技巧

    使用android studio开发项目的一些问题,功能和技巧. 1. 环境 Mac OSX 10.9.5 + Android Studio 0.8.9 2. gradle项目加载超慢 这是因为gra ...

  3. 我的c++学习(2)比较两个数字大小

    #include "stdafx.h" #include<iostream> using namespace std; int max(int i, int j){ / ...

  4. iOS Outlets Referencing Outlets

    一位网友的解释: 原址:http://www.cocoachina.com/bbs/read.php?tid-21295.html

  5. ural 1252. Sorting the Tombstones

    1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...

  6. 编程思想┊从实例谈面向对象编程(OOP)、工厂模式和重构

    有了翅膀才能飞,欠缺灵活的代码就象冻坏了翅膀的鸟儿.不能飞翔,就少了几许灵动的气韵.我们需要给代码带去温暖的阳光,让僵冷的翅膀重新飞起来.结合实例,通过应用OOP.设计模式和重构,你会看到代码是怎样一 ...

  7. 【转】CentOS6.3安装Broadcom无线网卡驱动

    转自: http://blog.csdn.net/jimanyu/article/details/9697833 下面是具体的步骤 一:确定无线网卡的型号,驱动下载 第一步要确定机子的无线网卡型号是什 ...

  8. 支持向量机(SVM)

    断断续续看了好多天,赶紧补上坑. 感谢july的 http://blog.csdn.net/v_july_v/article/details/7624837/ 以及CSDN上淘的比较正规的SMO C+ ...

  9. Android 解压缩功能

    主要用到zip: import java.util.Enumeration; import java.util.zip.CRC32; import java.util.zip.CheckedOutpu ...

  10. 洛谷 P1546 最短网络 Agri-Net Label:Water最小生成树

    题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...