iframe,div弹出层问题 我在iframe页面中有一个按钮,当点击按钮是弹出一个div层覆盖整个页面

我想知道怎样做使弹出层覆盖整个父页面

第1个回答  2011-11-14
亲测可以的,如下:
test1.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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>test1</title>
</head>
<body>
<iframe width="200px" height="200px" src="test2.html"></iframe>
</body>
</html>
test2.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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>test2</title>
<script>
function show(){
var div=document.createElement("div");
div.style.width=parent.document.documentElement.scrollWidth+"px";
div.style.height=parent.document.documentElement.scrollHeight+"px";
div.style.backgroundColor="red";
div.style.position="absolute";
div.style.left=0;
div.style.top=0;
div.style.zIndex=9999;
if(document.all)
div.style.filter = "alpha(opacity=30)";
else div.style.opacity = .3;

parent.document.body.appendChild(div);
}
</script>
</head>
<body>
<input type="button" value="测试" onclick="show()" />
</body>
</html>

当然这个代码也不是完整的一个遮罩效果代码,可能还有一点点浏览器兼容性问题存在。追问

那这个弹出层就把iframe中的内容全部遮住了,我想要的是,弹出的层把整个父页面遮住,但还有一个message的div呢,这个也遮住了,这个有办法实现吗

追答

这个简单,正常的遮罩效果都是有一个不被挡住的div,我现在是做了个简单的。
在test1.html中加入下面的div:

这样这个div就在上面档不住了。

追问

可这个不遮住的div中的内容在iframe页面中啊

追答

这样的话就稍微麻烦点,要想不被挡住只能把这个div添加到父页面。
然后使用父页面调用子页面函数的方法来实现操作子页面。
iframe的父页面操作子页面网上很多的,找下就知道了。

本回答被提问者采纳
第2个回答  2011-11-14
用iframe的话没有办法的。追问

用别的有可以实现这种效果的?

追答

如下是可以的(注意用到的 top.document):

test1.html

test1

test2.html

test2

function show(){
var div=top.document.createElement("div");

div.style.width=top.document.documentElement.scrollWidth+"px";
div.style.height=top.document.documentElement.scrollHeight+"px";

div.style.backgroundColor="red";
div.style.position="absolute";
div.style.left=0;
div.style.top=0;
div.style.zIndex=9999;

if(top.document.all)
div.style.filter = "alpha(opacity=30)";
else div.style.opacity = .3;
alert(top.document.getElementById("ddd").id);
top.document.getElementById("ddd").appendChild(div);
}

相似回答