手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jQuery取得iframe中元素的常用方法详解
jQuery取得iframe中元素的常用方法详解
摘要:本文实例分析了jQuery取得iframe中元素的常用方法。分享给大家供大家参考,具体如下:jquery取得iframe中元素的几种方法:在...

本文实例分析了jQuery取得iframe中元素的常用方法。分享给大家供大家参考,具体如下:

jquery取得iframe中元素的几种方法:

在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()

用JS或jQuery访问页面内的iframe,兼容IE/FF

注意:框架内的页面是不能跨域的!

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

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

<!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 内容:

<!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.jb51.net</div> </body> </html>

1. 在index.html执行JS直接访问:

复制代码 代码如下:document.getElementById('koyoz').contentWindow.document.getElementById('test').style.color='red'

通过在index.html访问ID名为'koyoz'的iframe页面,并取得此iframe页面内的ID为'test'的对象,并将其颜色设置为红色.

此代码已经测试通过,能支持IE/firefox .

2. 在index.html里面借助jQuery访问:

复制代码 代码如下:$("#koyoz").contents().find("#test").css('color','red');

此代码的效果和JS直接访问是一样的,由于借助于jQuery框架,代码就更短了.

收集网上的一些示例:

用jQuery在IFRAME里取得父窗口的某个元素的值只好用DOM方法与jquery方法结合的方式实现了

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

复制代码 代码如下:$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");

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

复制代码 代码如下:$(window.parent.document).find("input:radio").attr("checked","true");

父窗口想获得IFrame中的Iframe,就再加一个frames子级就行了,如:

复制代码 代码如下:$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");

希望本文所述对大家jQuery程序设计有所帮助。

【jQuery取得iframe中元素的常用方法详解】相关文章:

jQuery获取字符串中出现最多的数

动态加载jQuery的方法

js中跨域方法原理详解

JavaScript中的toUTCString()方法使用详解

JavaScript中的Math.LN2属性用法详解

JQuery中DOM事件绑定用法详解

Jquery中基本选择器用法实例详解

jQuery替换textarea中换行的方法

jQuery基于图层模仿五星星评价功能的方法

JavaScript中的setUTCDate()方法使用详解

精品推荐
分类导航