javascript关于Iframe父窗口和子窗口交互

//兼容IE、Firefox的iframe DOM获取函数
function getIFrameDOM(id){
	return document.getElementById(id).contentDocument || document.frames[id].document;
}


parent.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=utf-8" />

</head>
<body>
<div id="pHello" style="margin:10px auto;width:360px;height:30px;">此处可通过iframeB的JavaScript函数,来替换哦~</div>
<iframe id="wIframeA" name="myiframeA" src="a.html" scrolling="no" frameborder="1"></iframe>
<iframe id="wIframeB" name="myiframeB" src="b.html" scrolling="no" frameborder="1"></iframe>
<div style="margin:10px auto;width:300px;height:40px;">
<input type="button" value="父窗口操作iframeA" οnclick="iframeA();" style="width:150px;float:left;"/>
<input type="button" value="父窗口操作iframeB" οnclick="iframeB();" style="width:150px;float:left;"/>
</div>
<script type="text/javascript">
function iframeA(){
	var a = getIFrameDOM("wIframeA");
	a.getElementById('hello_a').style.background = "red";
}
function iframeB(){
	var a = getIFrameDOM("wIframeB");
	a.getElementById('hello_b').style.background = "green";
}
function getIFrameDOM(id){
	return document.getElementById(id).contentDocument || document.frames[id].document;
}
</script>
</body>
</html>

a.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=utf-8" />

</head>
<body>
<div id="hello_a">Hello a.html</div>
<script>
	
</script>
</body>
</html>

b.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=utf-8" />
</head>
<body>
<div id="hello_b">Hello b.html</div>
<input type="button" value="子窗口操作父窗口" οnclick="parentIframe();" style="width:150px;float:left;"/>
<input type="button" value="子窗口操作iframeA" οnclick="iframeA();" style="width:150px;float:left;"/>
<script type="text/javascript">
function iframeA(){
	var a = parent.getIFrameDOM("wIframeA");
	a.getElementById('hello_a').style.background = "blue";
}
function parentIframe(){
	parent.document.getElementById("pHello").innerHTML = "<p style='background:#000;color:#fff;font-size:15px;'>O(∩_∩)O哈哈~用子窗口B就可以替换你!不服吗?</p>";
}
</script>
</body>
</html>

注意:

--------------------------------------------

在firefox下,iframe背景默认的是透明的,在IE下默认不是透明的,

我们可以使用allowTransparency='true' 来设置IE下的iframe为透明,

另外我们使用scrolling ='no' frameborder='0'分别来实现iframe页面没有滚动条,边框宽度。


动态将Iframe背景置为透明

iframeObj.allowTransparency = true;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值