子页面访问父页面和跨iframe访问

http://xqin.cn/temp/iframe_a.html
[url=http://www.riaos.com]RIA知识库[/url]
[url=http://www.riaos.com]flex[/url]
[url=http://www.riaos.com]RIA[/url]

function copyValue(aim, original){
aim.value=window.parent.parent.document.getElementById('customIf').contentWindow.getValue(original);

/*
document.getElementById("frame1").contentDocument
alert(window.parent.parent.document.getElementById('customIf').contentDocument.getElementById('hjbs').value);
alert(window.parent.parent.document.getElementById("MyHotFrame"));
alert(window.parent.parent.document.getElementById('userTa').align);
aim.value=document.getElementById(original).value;
alter(window.parent.parent.document.getElementById('customIf').height);//.document.getElementById('hjbs').value);
*/
}



<!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>
<title>子父双向操作</title>
<script type="text/javascript">
//<![CDATA[
function test(){
var r = Math.random();
show(r);
show.call(who, r);
return false;
}
function show(m){
this.document.getElementById('message').innerHTML = m;
}
var who = null;
window.onload = function(){
who = document.getElementById('ifr').contentWindow;
}
//]]-->
</script>
</head>

<body>
<div id="message"></div>

<input type="button" onclick="test()" value="测试" />
<hr/>
<iframe src="iframe_b.html" width="400" height="200" id="ifr"></iframe>
<pre>
<h1>简要说明</h1>

如果被嵌入的页面不需要去对父窗口做任何事件,
那么在iframe_b.html时面的script这一段就可以删除掉.
反过来也是如此,既子窗口不需要父窗口做任何事件,那么iframe_a.html里面的Script代码块就可以删除掉.

在框架内(外)做同样的事情,可以用 functoin对象的call和applay方法来实现,不同window情况下的切换.

既,show函数在用call(who,r)之后,在函数内部的this就指向 who 所设置的对象,在这个页面它指向的是内部的iframe,所以就改变了 iframe_b.html 里面的message的内容.

在iframe_b里面它指向parent,所以在 iframe_b里面调用的时候,就改变了父窗口的 message的内容.
</pre>
</body>

</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>
<script type="text/javascript">
//<![CDATA[
function test(){
var r = Math.random();
show(r);
show.call(who, r);
return false;
}
function show(m){
this.document.getElementById('message').innerHTML = m;
}
var who = null;
window.onload = function(){
who = parent;
}
//]]-->
</script>
</head>

<body>
<div id="message"></div>
<input type="button" onclick="test()" value="测试" />
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值