简单设置iframe的背景色及填充iframe
前几天在做一个自定义弹出层时,需要用到iframe做半透明的背景,但是由于iframe不能直接设置背景色而苦恼,百度google之后发现人们大都是给iframe设置src指向一个目标html页面,此页面仅仅是在body中设置背景色。始终觉得这样子不太好,一来是是iframe中页面缓存十分严重,日后修改背景色麻烦,二来如果加访问地址随机的话会导致每次弹框都要刷新。
我的方法:<iframe src="javascript:document.write('<body style=\'background-color:black\'></body>')"></iframe>
这样一来,我们不仅仅只可以设置iframe的背景色了,还可以做很多事,如有时候要在iframe中放一个简单的图片,写几行字等等,也没必要再去写一个页面来做指向了。
下面是一个完整的示例:
<html>
<head>
<script type="text/javascript">
function ggyy()
{
var code = ["<html>"];
code.push("<body style='color:Red;'>");
code.push("<img src='http://www.baidu.com/img/baidu_logo.gif'/>");
code.push("<span style='color:Red;'>这是iframe中的字</span>");
code.push("</body>");
code.push("</html>");
return code.join("");
}
</script>
</head>
<body>
<iframe width="700px" height="400px" src="javascript:parent.ggyy()"></iframe>
</body>
</html>
<head>
<script type="text/javascript">
function ggyy()
{
var code = ["<html>"];
code.push("<body style='color:Red;'>");
code.push("<img src='http://www.baidu.com/img/baidu_logo.gif'/>");
code.push("<span style='color:Red;'>这是iframe中的字</span>");
code.push("</body>");
code.push("</html>");
return code.join("");
}
</script>
</head>
<body>
<iframe width="700px" height="400px" src="javascript:parent.ggyy()"></iframe>
</body>
</html>
posted on 2009-12-22 16:36 Handsome Kun 阅读(2138) 评论(0) 编辑 收藏 举报