1)在页面中查找 iframe 页面元素:
$(‘#iframe’).contents().find(‘#id’)
2)在 iframe 中查找父页面元素:
$(‘#id’, parent.document)
3)在 iframe 中调用父页面中定义的方法和变量:
parent.method
parent.value
父页面
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<div id=
"div"
>div element</div>
<iframe id=
"iframe"
src=
"page.html"
frameborder=
"0"
></iframe>
<script src=
"http://code.jquery.com/jquery-latest.min.js"
></script>
<script>
$(
function
($) {
//在页面中查找 iframe 页面元素
var
p = $(
'#iframe'
).contents().find(
'#p'
).text();
alert(p);
});
//自定义变量
var
hello =
'hello'
;
//自定义方法
function
getHelloWorld() {
alert(
'hello world'
);
}
</script>
|
子页面
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<p id=
"p"
>p element</p>
<script src=
"http://code.jquery.com/jquery-latest.min.js"
></script>
<script>
$(
function
($) {
//在 iframe 中查找父页面元素
alert($(
'#div'
, parent.document).text());
//在 iframe 中调用父页面中定义的方法
parent.getHelloWorld();
//在 iframe 中调用父页面中定义的变量
alert(parent.hello);
});
</script>
|
- <a href="logout.html" target="_parent">退出系统</a>
本文详细介绍了如何在网页中查找并操作iframe页面元素,包括查找iframe内部元素、调用父页面定义的方法和访问父页面定义的变量。通过示例代码展示了如何在iframe中实现这些操作。
325

被折叠的 条评论
为什么被折叠?



