<script>
function a()
{
.
.
.
for(var i=0;i<xx,i++)
{
//cell3是动态生成的<td></td>(一列)
//动态生成iframe,
var cea = document.createElement("iframe");
cea.id = 'cwin'+i;
cea.scrolling = 'no';
cea.src = link;
cea.width = '100%';
cell3.appendChild(cea);
var win='cwin'+i;
//alert(win); 当我把这句话加入时,运行正确,如果注释掉这句话,就报错:没有对象,行数:80(蓝色那行)。
SetCwinHeight(win);
}
}//iframe自适应高度
function SetCwinHeight(win)
{
var cwin=document.getElementById(win);
cwin.height=cwin.Document.body.scrollHeight; --80行
}</script><body onload="a()">
function a()
{
.
.
.
for(var i=0;i<xx,i++)
{
//cell3是动态生成的<td></td>(一列)
//动态生成iframe,
var cea = document.createElement("iframe");
cea.id = 'cwin'+i;
cea.scrolling = 'no';
cea.src = link;
cea.width = '100%';
cell3.appendChild(cea);
var win='cwin'+i;
//alert(win); 当我把这句话加入时,运行正确,如果注释掉这句话,就报错:没有对象,行数:80(蓝色那行)。
SetCwinHeight(win);
}
}//iframe自适应高度
function SetCwinHeight(win)
{
var cwin=document.getElementById(win);
cwin.height=cwin.Document.body.scrollHeight; --80行
}</script><body onload="a()">
<script>
function a()
{
.
.
.
for(var i=0;i <xx,i++)
{
//cell3是动态生成的 <td> </td> (一列)
//动态生成iframe,
var cea = document.createElement("iframe");
cea.id = 'cwin'+i;
cea.scrolling = 'no';
cea.src = link;
cea.width = '100%';
cell3.appendChild(cea);
var win='cwin'+i;
//alert(win); 当我把这句话加入时,运行正确,如果注释掉这句话,就报错:没有对象,行数:80(蓝色那行)。
setTimeout("SetCwinHeight("+win+");",1000);//延迟一下试试,可能是动态加载元素还没结束。
}
} //iframe自适应高度
function SetCwinHeight(win)
{
var cwin=document.getElementById(win);
cwin.height=cwin.Document.body.scrollHeight; --80行
} </script> <body onload="a()">
改下试试:<script>
function a()
{
.
.
.
for(var i=0;i <xx,i++)
{
//cell3是动态生成的 <td> </td> (一列)
//动态生成iframe,
var cea = document.createElement("iframe");
cea.id = 'cwin'+i;
cea.scrolling = 'no';
cea.src = link;
cea.width = '100%';
cell3.appendChild(cea);
var win='cwin'+i;
//alert(win); 当我把这句话加入时,运行正确,如果注释掉这句话,就报错:没有对象,行数:80(蓝色那行)。
SetCwinHeight(win);
}
} //iframe自适应高度
function SetCwinHeight(win)
{
var cwin=document.getElementById(win);
if(cwin.document.body)cwin.height=cwin.document.body.scrollHeight; --80行
else setTimeout("setCwinHeight("+win+");",1000);
} </script> <body onload="a()">
function a()
{
.
.
.
for(var i=0;i <xx,i++)
{
//cell3是动态生成的 <td> </td> (一列)
//动态生成iframe,
var cea = document.createElement("iframe");
cea.id = 'cwin'+i;
cea.scrolling = 'no';
cea.src = link;
cea.width = '100%';
cell3.appendChild(cea);
var win='cwin'+i;
//alert(win); 当我把这句话加入时,运行正确,如果注释掉这句话,就报错:没有对象,行数:80(蓝色那行)。
setTimeout("SetCwinHeight("+win+");",1000);//延迟一下试试,可能是动态加载元素还没结束。
}
}
还有一种可能性:你原帖中cwin.document写成了cwin.Document。是不是缺乏Document这个对象。
你自己再查查吧。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>menugoogle1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type=text/javascript>
var xmlHttp;
function pageload()
{
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET","content1.xml",true);
xmlHttp.send(null);
}
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
ListMenu1(); //第一階Menuを表示する。
}
}
}
function ListMenu1()
{
var mainmenu = xmlHttp.responseXML;
var menunode = mainmenu.getElementsByTagName("content");
CreateRow(menunode); //functionを引用する、機能は第一階Menuを作る
}
function CreateRow(menu)
{
document.getElementById("newTable").innerHTML=document.getElementById("newTable").innerHTML+"<table id=tb11 align=center border=0 width=100% cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#000099\"></table>";
var tdData1 = document.getElementById("tb11");
len=menu.length;
var Title="title";
var Link="link";
var L_img="Limg";
var C_img="Cimg";
var R_img="Rimg";
for(var i=0;i<len;i++)
{
content=menu[i];
var title=getElement(content,Title);
var link=getElement(content,Link);
var L=getElement(content,L_img);
var C=getElement(content,C_img);
var R=getElement(content,R_img);
//alert(R);
//alert(a);
var newRow=tdData1.insertRow(2*i);//新行を追加する
newRow.height=35;
//newRow.bgcolor="#99ccff;";
newRow.border=0;
var cell0=newRow.insertCell(0);//列を追加する
cell0.align = "right";
cell0.id=2*i;
cell0.style.cssText="width:2%;FONT-WEIGHT: bold;color:white;";
cell0.background=L;
//cell0.border=0;
var cell1=newRow.insertCell(1);
cell1.align="center";
//cell1.valign="top";
//cell1.width="96%";
//cell1.id=
cell1.innerText=title;
cell1.style.cssText="font-size:18px;FONT-WEIGHT: bold;";
cell1.background=C;
cell1.border=0;
var cell2=newRow.insertCell(2);
//
cell2.align="left";
cell2.style.cssText="width:3%;FONT-WEIGHT: bold;";
cell2.background=R;
cell2.border=0;
var newRow=tdData1.insertRow(2*i+1);//新行を追加する
newRow.height=300;
//newRow.border=0;
var cell3=newRow.insertCell(0);
cell3.align="center";
cell3.valign="top";
cell3.id=2*i+1;
cell3.colSpan=3;
//alert(link);
//cell3.innerHTML='<iframe border=0 id=cwin'+i+' width=100% scrolling=no align=top src='+link+'></iframe>';
var cea = document.createElement("iframe");
cea.id = 'cwin'+i;
cea.scrolling = 'no';
cea.src = link;
cea.width = '100%';
cell3.appendChild(cea);
var win='cwin'+i;
alert(win);
SetCwinHeight(win);
}
f=1;
}
function getElement(menu,ele)
{
if(menu.getElementsByTagName(ele)[0])
{
if(menu.getElementsByTagName(ele)[0].firstChild)
return menu.getElementsByTagName(ele)[0].firstChild.nodeValue;
else
return "";
}
else
{
return "";
}
}
function SetCwinHeight(len)
{
//var cwin=document.getElementById("cwin");
//alert(win);
var cwin=document.getElementById(win);
cwin.height=cwin.Document.body.scrollHeight;
/////cwin.width=cwin.Document.body.scrollWidth;
/*for(var i=0;i<len;i++)
{
//alert(f);
var win='cwin'+i;
//alert(win);
var cwin1=document.getElementById(win);
cwin1.height=cwin1.Document.body.scrollHeight;
}
//var cwin=document.getElementById("cwin0");
//alert(1);
//cwin.height=cwin.Document.body.scrollHeight;*/
}
</script>
</HEAD>
<body onload="pageload()" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="newTable" align="center" style="OVERFLOW-Y: auto; Z-INDEX: 101; WIDTH: 100%; CURSOR: hand; POSITION: absolute; TOP: 10px; HEIGHT: 100%; BACKGROUND-COLOR: #ffffff">
<!--table id="tb11" align=center style="WIDTH: 32px; CURSOR: hand; POSITION: absolute; TOP: 2px; HEIGHT: 500px"
bgColor="#99ccff" border="1" bordercolor="#000033">
</table-->
</div>
</form>
</body>
</HTML>
这是全部的代码。指教一下。楼上说话注意点,
//alert(wwin);
SetCwinHeight(wwin);
function SetCwinHeight(len)--->SetCwinHeight(win)
{
//var cwin=document.getElementById("cwin");
//alert(win);
var cwin=document.getElementById(win);----->改为ccwin=document.getElementById(win)
cwin.height=ccwin.Document.body.scrollHeight;你用这些试试也可以参考这个例子<body>
<iframe marginheight="0" marginwidth="0" src="Default.aspx" frameborder="0" scrolling="no" width="100%" id="info_iframe" name="info_content" onload="change_size()"></iframe>
</body><head>
<script language="javascript">
function change_size()
{
document.getElementById("info_iframe").style.height=info_content.document.body.scrollHeight+1+"px";
}
</script>
</head>
cwin.height=ccwin.Document.body.scrollHeight; ----〉你的意思是不是改为:ccwin.height=ccwin.Document.body.scrollHeight;
我试了,不行。
另外你的下一个方法也试了,还是不行。估计应该是我的iframe是动态生成的原因。只要在代码的中间加上一个alert语句就可以顺利执行。
比如:
var win='cwin'+i;
alert(1); ----〉加上这一句就能执行。
SetCwinHeight(win);
估计是javascript代码同步执行的原因。但是用延时功能也不能解决。
2、25楼的问题不在于child语法,parent.document.body存在,而cwin.document.body没有装载完毕。这才是关键啊。
3、即使你需要像25楼那样干,也不需要child,因为parent的child就是自己啊。
所应该是他的child。
汗!
所应该是他的child。
=================
他的高度就是document.getElementById(yourIframeId).window.document.body.scrollHeight.
{
var cwin=document.getElementById(win);
if(cwin.window.document.body)cwin.height=cwin.window.document.body.scrollHeight; else setTimeout("setCwinHeight("+win+");",1000);
} 如果还不行,那就用下面的代码找找原因,看到底是确哪个对象:SetCwinHeight(win)
{
try{
var cwin=document.getElementById(win);
cwin.height=cwin.window.document.body.scrollHeight; }
}catch(e){
var errstr = e.message;
if(cwin)errstr += "\ncwin:"+cwin;
else errstr += "\ncwin:null";
if(cwin.window)errstr += "\nwindow:"+cwin.window;
else errstr +="\nwindow:null";
if(cwin.window.document)errstr += "\ndocument:"+cwin.window.document;
else errstr +="\ndocument:null";
if(cwin.window.document.body)errstr += "\nbody:"+cwin.window.document.body;
else errstr +="\ndbody:null";
if(cwin.window.document.body.scrollHeight)errstr += "\nheight:"+cwin.window.document.body.height;;
else errrstr += "\nheight:null";
}
下面的代码使了一下。SetCwinHeight(win)
{
try{
var cwin=document.getElementById(win);
cwin.height=cwin.window.document.body.scrollHeight; } //这个应该是不需要的吧
}catch(e){
var errstr = e.message;
if(cwin)errstr += "\ncwin:"+cwin;
else errstr += "\ncwin:null";
if(cwin.window)errstr += "\nwindow:"+cwin.window;
else errstr +="\nwindow:null";
if(cwin.window.document)errstr += "\ndocument:"+cwin.window.document;
else errstr +="\ndocument:null";
if(cwin.window.document.body)errstr += "\nbody:"+cwin.window.document.body;
else errstr +="\ndbody:null";
if(cwin.window.document.body.scrollHeight)errstr += "\nheight:"+cwin.window.document.body.height;;
else errrstr += "\nheight:null";
}执行后,直接白屏。报错是<body onload="pageload()">这行,错误:请指定对象。
我没调试过javascript,不知怎么调试。
SetCwinHeight(win)
{
try{
var cwin=document.getElementById(win);
cwin.height=cwin.window.document.body.scrollHeight;
}catch(e){
var errstr = e.message;
if(cwin)errstr += "\ncwin:"+cwin;
else errstr += "\ncwin:null";
if(cwin.window)errstr += "\nwindow:"+cwin.window;
else errstr +="\nwindow:null";
if(cwin.window.document)errstr += "\ndocument:"+cwin.window.document;
else errstr +="\ndocument:null";
if(cwin.window.document.body)errstr += "\nbody:"+cwin.window.document.body;
else errstr +="\ndbody:null";
if(cwin.window.document.body.scrollHeight)errstr += "\nheight:"+cwin.window.document.body.height;;
else errstr += "\nheight:null";
alert(errstr);
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>menugoogle1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript">
var xmlHttp;
var frmid = new Array();
function pageload()
{
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET","content1.xml",true);
xmlHttp.send(null);
}
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
ListMenu1();
}
}
}
function ListMenu1()
{
var mainmenu = xmlHttp.responseXML;
var menunode = mainmenu.getElementsByTagName("content");
CreateRow(menunode);
}
//从这儿开始是动态添加控件的方法 function CreateRow(menu)
{
document.getElementById("newTable").innerHTML=document.getElementById("newTable").innerHTML+"<table id=tb11 align=center border=0 width=100% cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#000099\"></table>";
var tdData1 = document.getElementById("tb11");
///从xml读取的数据
len=menu.length;
var Title="title";
var Link="link";
var L_img="Limg";
var C_img="Cimg";
var R_img="Rimg";
for(var i=0;i<len;i++)
{
content=menu[i];
var title=getElement(content,Title);
var link=getElement(content,Link);
var L=getElement(content,L_img);
var C=getElement(content,C_img);
var R=getElement(content,R_img);
//alert(R);
//alert(a);
var newRow=tdData1.insertRow(2*i); //对table添加行和列
newRow.height=35;
//newRow.bgcolor="#99ccff;";
newRow.border=0;
var cell0=newRow.insertCell(0);
cell0.align = "right";
//cell0.valign="top";
//cell0.innerText=menutxt;
//cell0.width=1;
cell0.id=2*i;
cell0.style.cssText="width:2%;FONT-WEIGHT: bold;color:white;";
cell0.background=L;
//cell0.border=0;
var cell1=newRow.insertCell(1);
cell1.align="center";
//cell1.valign="top";
//cell1.width="96%";
//cell1.id=
cell1.innerText=title;
cell1.style.cssText="font-size:18px;FONT-WEIGHT: bold;";
cell1.background=C;
cell1.border=0;
var cell2=newRow.insertCell(2);
//
cell2.align="left";
//cell2.width=1;
//cell2.valign="top";
//cell1.id=
cell2.style.cssText="width:3%;FONT-WEIGHT: bold;";
cell2.background=R;
cell2.border=0;
//var Frame = document.getElementById("frame");
//alert(Frame);
//Frame.src=link;
//x=this.parentElement.rowIndex;
//var a=document.getElementById(x).innerHTML;
//alert(a);
//document.getElementById("frame").setAttribute("src")="http://www.baidu.com";
var newRow=tdData1.insertRow(2*i+1);
newRow.height=300;
//newRow.border=0;
var cell3=newRow.insertCell(0);
cell3.align="center";
cell3.valign="top";
cell3.id=2*i+1;
//cell3.width="80%";
cell3.colSpan=3;
//alert(link);
//cell3.innerHTML='<iframe border=0 id=cwin'+i+' width=100% scrolling=no align=top src='+link+'></iframe>';
var cea = document.createElement("iframe");//添加iframe并设置属性
cea.id = 'cwin'+i;
cea.Name='win'+i;
cea.scrolling = 'no';
cea.src = link;
cea.width = '100%';
//cea.height=child.document.body.scrollHeight;
cell3.appendChild(cea);
frmid[i]='cwin'+i;
alert(1);
//cea.height='100%';
//cea.innerHeight='100%';
//cea.height=cea.scrollHeight;
//alert(cea);
//var objwin=cea.contentWindow;
//alert(objwin);
//cea.height=parent.Document.body.scrollHeight;
//cea.on = new Function("alert(11);");
//alert(cea.innerHeight);
/*cell3.style.cssText="";
var cell4=newRow.insertCell(1);
var cell5=newRow.insertCell(2);
var a=document.getElementById("tb11").innerHTML;
alert(a);
var Frame = document.getElementById("frame");
Frame.src=link;
//var dd=setTimeout('SetCwinHeight('+win+');',1000);
//alert(win);
//alert(cell3.innerHTML);
var n='win'+i;
var wwin='cwin'+i;
//alert(1);
SetCwinHeight(wwin);*/
}
for(var j=0;j<frmid.length;j++)
{
//var wwin='cwin'+i;
SetCwinHeight(frmid[j]);
}
}
function getElement(menu,ele)
{
if(menu.getElementsByTagName(ele)[0])
{
if(menu.getElementsByTagName(ele)[0].firstChild)
return menu.getElementsByTagName(ele)[0].firstChild.nodeValue;
else
return "";
}
else
{
return "";
}
}
//自适应高度函数
function SetCwinHeight(win)
{
//var cwin=document.getElementById("cwin");
//try
//{ alert(win);
var cwin=document.getElementById(win);
//if(ccwin.document.body)
cwin.height=cwin.Document.body.scrollHeight;
/*}
catch(e)
{
var errstr = e.message;
if(cwin)errstr += "\ncwin:"+cwin;
else errstr += "\ncwin:null";
if(cwin.window)errstr += "\nwindow:"+cwin.window;
else errstr +="\nwindow:null";
if(cwin.window.Document)errstr += "\ndocument:"+cwin.window.Document;
else errstr +="\nDocument:null";
if(cwin.window.window.Document.body)errstr += "\nbody:"+cwin.Document.body;
else errstr +="\ndbody:null";
if(cwin.Document.body.scrollHeight)errstr += "\nheight:"+cwin.Document.body.height;;
else errstr += "\nheight:null";
alert(errstr);
}*/
/*//alert(win);
//alert(nn);
//else
//setTimeout("SetCwinHeight("+win+");",1000);
/////cwin.width=cwin.Document.body.scrollWidth;
for(var i=0;i<len;i++)
{
//alert(f);
var win='cwin'+i;
//alert(win);
var cwin1=document.getElementById(win);
cwin1.height=cwin1.Document.body.scrollHeight;
}
//var cwin=document.getElementById("cwin0");
//alert(1);
//cwin.height=cwin.Document.body.scrollHeight;*/
}
</script>
</HEAD>
<body onload="pageload();" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="newTable" align="center" style="OVERFLOW-Y: auto; Z-INDEX: 101; WIDTH: 100%; CURSOR: hand; POSITION: absolute; TOP: 10px; HEIGHT: 100%; BACKGROUND-COLOR: #ffffff">
<!--table id="tb11" align=center style="WIDTH: 32px; CURSOR: hand; POSITION: absolute; TOP: 2px; HEIGHT: 500px"
bgColor="#99ccff" border="1" bordercolor="#000033">
</table-->
</div>
<FONT face="MS UI Gothic"></FONT><FONT face="MS UI Gothic"></FONT>
</form>
</body>
</HTML>
{
try{
var cwin=document.getElementById(win);
cwin.height=cwin.window.document.body.scrollHeight;
}catch(e){
var errstr = e.message;
if(cwin)errstr += "\ncwin:"+cwin;
else errstr += "\ncwin:null";
if(cwin.window)errstr += "\nwindow:"+cwin.window;
else errstr +="\nwindow:null";
if(cwin.window.document)errstr += "\ndocument:"+cwin.window.document;
else errstr +="\ndocument:null";
if(cwin.window.document.body)errstr += "\nbody:"+cwin.window.document.body;
else errstr +="\ndbody:null";
if(cwin.window.document.body.scrollHeight)errstr += "\nheight:"+cwin.window.document.body.height;;
else errstr += "\nheight:null";
setTimeout("SetCwinHeight("+win+")",1000);
alert(errstr);
}
}
还有一个办法,你可以把cea.src = link; 先去掉,改成cea.innerHTML = '<html><body></body></html>',看问题是不是消失。如果消失,说明确实是URL指定的HTML的问题。那就改好就成啦。
另外,改成cea.innerHTML = ' <html> <body> </body> </html> ' 之后,他以后的代码,就都不执行了。