
首先你要能获取到这个iframe的dom对象,假设我们知道它的ID;
var frame = documentgetElementById("ueditor_0");var frameDocument = framecontentWindowdocument;
iframe中的document对象拿到了,后边如何获取你想要的元素就很简单了吧。
你好,Document 对象包括HTML页面所有的对象,XML页面说有的对象。这些都是于Document
的内容,也就是根节点中包含的东西都可以用document获取。另外document获取对象的时候是通过方法来获取的,不可以直接对象名吧。
query取得iframe中元素的几种方法
在iframe子页面获取父页面元素
代码如下:
$('#objId', parentdocument);
// 搞定
在父页面 获取iframe子页面的元素
代码如下:
$("#objid",documentframes('iframename')document)
$(documentgetElementById('iframeId')contentWindowdocumentbody)html()
显示iframe中body元素的内容。
$("#testId", documentframes("iframename")document)html();
根据iframename取得其中ID为"testId"元素
$(windowframes["iframeName"]document)find("#testId")html()
用JS或jQuery访问页面内的iframe,兼容IE/FF
注意:框架内的页面是不能跨域的!
假设有两个页面,在相同域下
indexhtml 文件内含有一个iframe:
XML/HTML代码
">
iframe:
content: '<iframe id="iframe1" src="" style="" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>'
方法:
var a = documentgetElementById('iframe1')contentWindowdocumentgetElementById('a)value; //输入值
var a= documentgetElementById('iframe1')contentWindowdocumentgetElementById('a')defaultValue; //默认值
一、关于 Document 对象
Document 对象
每个载入浏览器的 HTML 文档都会成为 Document 对象。
Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。
提示:Document 对象是 Window 对象的一部分,可通过 windowdocument 属性对其进行访问。
二、如何获取文档对象上的元素
通过 document 获取网页元素,有多种方法,简述如下:
1、通常可以使用 getElementById、getElementByName 等方法:
示例代码:
function alertValue(){
alert(documentgetElementById("text1")value)
}
2、通过 document 对象的元素结合:
在父窗口中获取iframe中的元素
1、
格式:windowframes["iframe的name值"]documentgetElementByIdx_x("iframe中控件的ID")click();
实例:windowframes["ifm"]documentgetElementByIdx_x("btnOk")click();
2、
格式:
var obj=documentgetElementByIdx_x("iframe的name")contentWindow;
var ifmObj=objdocumentgetElementByIdx_x("iframe中控件的ID");
ifmObjclick();
实例:
var obj=documentgetElementByIdx_x("ifm")contentWindow;
var ifmObj=objdocumentgetElementByIdx_x("btnOk");
ifmObjclick();
在iframe中获取父窗口的元素
格式:windowparentdocumentgetElementByIdx_x("父窗口的元素ID")click();
实例:windowparentdocumentgetElementByIdx_x("btnOk")click();
jquery
在父窗口中获取iframe中的元素
1、
格式:$("#iframe的ID")contents()find("#iframe中的控件ID")click();//jquery 方法1
实例:$("#ifm")contents()find("#btnOk")click();//jquery 方法1
2、
格式:$("#iframe中的控件ID",documentframes("frame的name")document)click();//jquery 方法2
实例:$("#btnOk",documentframes("ifm")document)click();//jquery 方法2
在iframe中获取父窗口的元素
格式:$('#父窗口中的元素ID', parentdocument)click();
实例:$('#btnOk', parentdocument)click();
父窗获取子窗口的IFrame中的JS方法
一、父窗口调用iframe子窗口方法
1、HTML语法:<iframe name="myFrame" src="childhtml"></iframe>
2、父窗口调用子窗口:myFramewindowfunctionName();
3、子窗品调用父窗口:parentfunctionName();
简单地说,也就是在子窗口中调用的变量或函数前加个parent就行
4、父窗口页面源码:
复制代码代码如下:
<html>
<head>
<script type="text/javascript">
function say() {
alert("parenthtml------>I'm at parenthtml");
}
function callChild()
{
//documentframes("myFrame")f1();
myFramewindowsay();
}
</script>
</head>
<body>
<input type=button value="调用childhtml中的函数say()" onclick="callChild()">
<iframe name="myFrame" src="childhtml"></iframe>
</body>
</html>
5、子窗口页面:
复制代码代码如下:
<html>
<head>
<script type="text/javascript">
function say()
{
alert("childhtml--->I'm at childhtml");
}
function callParent() {
parentsay();
}
</script>
</head>
<body>
<input type=button value="调用parenthtml中的say()函数" onclick="callParent()">
</body>
</html>
二、iframe 父窗口和子窗口相互的调用方法
1、IE中使用方法:
父窗口调用子窗口:iframe_IDiframe_document_objectobject_attribute = attribute_value
例子:onClick="iframe_textmyH1innerText='>
以上就是关于js怎么在当前页面获取iframe中的元素全部的内容,包括:js怎么在当前页面获取iframe中的元素、想问一个关于JavaScript中的document对象的问题,、JavaScript或jquery获取frame的元素的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)