日期:2014-05-16  浏览次数:20421 次

如何改变iframe中所嵌页面的背景颜色
如题,我下面两个代码都不行啊
<iframe name="a" id="a" width="200" height="500" src="fffff.html"></iframe>
<script>
document.frames['a'].document.body.style.backgroundColor='red';
</script>

<iframe name="a" id="a" width="200" height="500" src="fffff.html"></iframe>
<script>
var edior=document.getElementById('a').contentWindow;
edior.document.body.style.backgroundColor='red';
</script>

------解决方案--------------------
问题的本质在于还未等iframe加载html完成就调用设置语句,所以无效
解决方法:
<script type="text/javascript"> 
AutoIframe(); 
function AutoIframe() 
{
if(document.readyState!= "complete") 

setTimeout(function(){AutoIframe();},25); 
return; 

else //控制在加载完成后执行设置

var edior=document.getElementById('a').contentWindow; 
edior.document.body.style.backgroundColor= 'red';


</script>

------解决方案--------------------
你也可以试试在<head></head>标签中加入以下代码:
<script>
window.onload=function()
{
var objFrame=document.getElementById("myFrame").contentWindow;
objFrame.document.body.style.backgroundColor="red";
}
</script>
==================================================================================
window.onload事件发生在页面加载完成后.所以以上代码应该能达到你想要的效果.