getElementByName();或者getElementById();
for(i=0;i<2;i++){
getElementByName("h"+i+"img").innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
}
for(i=0;i<2;i++){
getElementByName("h"+i+"img").innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
}
for(i=0;i<2;i++){
getElementByName("h"+i+"img").innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
}這樣就正常:
for(img=0;img<2;img++){
h1img.innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
}
for(i=0;i<2;i++){
h1img.innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
}
或
var imgObj=eval("h"+i+"img");
imgObj").innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
document.all("h"+i+"img").innerHTML = " ...(省略) ";
}
在代碼里我直接用h1img名字,對第一個記錄的圖標變換是正常的,如果有多記錄就要動態命名,但不成功,可能我代碼改得不好
可以幫我改代碼實現全部展開變換圖標效果。謝謝
=====================================================<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<SCRIPT luangage="javascript"><!--
function initIt1(){
if(this.name == "idbmainlist"){parent.changeColor('007CD0');}
tableColl=document.all.tags("tr");
for(i=0; i<tableColl.length; i++) {
whichEl=tableColl(i);
if(whichEl.className=="child" && whichEl.id !="$Cookies{which}"){whichEl.style.display="none";}
}
}
function expands1(el) {
expireDate=new Date;
expireDate.setYear(expireDate.getYear()+1);
whichEl1=eval(el+"Child");
whichimg=eval(el+"img");
var wh = (el+"Child");
if (whichEl1.style.display=="none"){
initIt1();
whichimg.innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
whichEl1.style.display="block";
document.cookie="which="+wh+";expires="+expireDate.toGMTString()+";";
}else{
whichimg.innerHTML="<img src='images/tree_folder4.gif' width='15' height='15' border='0'>";
whichEl1.style.display="none";
document.cookie="which="+";expires="+expireDate.toGMTString()+";";
}
}
var off_on=false;function open_all(){//全部展開或合閉功能
if(off_on){
off_on=false;
}else{
off_on=true;
}
if(this.name == "idbmainlist"){parent.changeColor('007CD0');}
tableColl=document.all.tags("tr");
for(i=0; i<tableColl.length; i++) {
whichEl=tableColl(i);
if(whichEl.className=="child" && whichEl.id !="$Cookies{which}" &&off_on){
for(img=0;img<2;img++){
h1img.innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
}
whichEl.style.display="block";
}
if(whichEl.className=="child" && whichEl.id !="$Cookies{which}" && !off_on){
for(img=0;img<2;img++){
h1img.innerHTML="<img src='images/tree_folder4.gif' width='15' height='15' border='0'>";
}
whichEl.style.display="none";
}
}
}
onload=initIt1;
//-->
</SCRIPT>
</head><body>
<table width="765" border="0" align="center" cellpadding="0" cellspacing="0" background="T_bg.gif">
<tr>
<td valign="bottom" background="T_bg.gif" style='BORDER-bottom: 1px double #dddddd;'><strong><font color="#333333" size="3">
</font></strong><font color="#0033FF" size="2" style="CURSOR: hand;" onclick="open_all();">全部展開<strong>
</strong></font> <a href="#" onClick="open_all();"><font color="#FFFFFF" size="3"></font></a></td>
</tr>
</table>
<table width="765" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#BEBEBE" bgcolor="#FFFCF4" id="AutoNumber2" style="border-collapse: collapse">
<tr align="center">
<td width="36" background="images/002bg.gif">序號</td>
<td width="725" height="25" background="images/002bg.gif">標題</td>
</tr>
<tr>
<td width="36" height="32" align="center" bgcolor="#FbFbFF"><a onclick=javascript:window.location='product_reply.asp?enter=59762&replyID=1446' title="問題:1" style="color:#0000ff;CURSOR: hand;" onmouseover="style.Color='#ff0000';" onmouseout="style.Color='#F5F5FF';">1</a></td>
<td bgcolor="#F5F5FF" style="CURSOR: hand;" onclick="javascript:expands1('h1')" onmouseover="javascript:this.bgColor='#E1E1FF'" onmouseout="javascript:this.bgColor='#F5F5FF'"><font color="#0000ff">
<font id="h1img"><img src="images/tree_folder4.gif" width="15" height="15" border="0"></font>《世界之家
/ 齊齊玩 - 30張咭套裝》存在問題 </font></td>
</tr>
<tr class=child id=h1Child>
<td height="16" colspan="2" align="center" valign="top" bgcolor="#FFFFFF">
<table width="763" border="0" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFCF4">
<td width="5%" rowspan="2"> </td>
</tr>
<tr bgcolor="#FFFCF4">
<td colspan="8" bgcolor="#FFFCF4" > 但我想動態命名,用下面這樣出錯,請問怎么樣才能動態命名? <br>
for(i=0;i<2;i++){<br>
h[i+"img"].innerHTML="<img src='images/tree_folder3.gif'
width='15' height='15' border='0'>";<br>
}</td>
</tr>
</table></td>
</tr>
<tr>
<td width="36" height="32" align="center" bgcolor="#FbFbFF"><a onclick=javascript:window.location='product_reply.asp?enter=59762&replyID=1451' title="問題:2" style="color:#0000ff;CURSOR: hand;" onmouseover="style.Color='#ff0000';" onmouseout="style.Color='#F5F5FF';">2</a></td>
<td bgcolor="#F5F5FF" style="CURSOR: hand;" onclick="javascript:expands1('h2')" onmouseover="javascript:this.bgColor='#E1E1FF'" onmouseout="javascript:this.bgColor='#F5F5FF'"><font color="#0000ff">
<font id="h2img"><img src="images/tree_folder4.gif" width="15" height="15" border="0"></font>指甲位啤到圖案
</font></td>
</tr>
<tr class=child id=h2Child>
<td height="16" colspan="2" align="center" valign="top" bgcolor="#FFFFFF">
<table width="763" border="0" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFCF4">
<td width="5%" rowspan="2"> </td>
</tr>
<tr bgcolor="#FFFCF4">
<td colspan="8" bgcolor="#FFFCF4" >這樣是正常的:<br>
h1img.innerHTML="<img src='images/tree_folder3.gif' width='15'
height='15' border='0'>";<br>
h2img.innerHTML="<img src='images/tree_folder3.gif' width='15'
height='15' border='0'>";</td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
for(i=0;i<2;i++){<br>
h[i+"img"].innerHTML="<img src='images/tree_folder3.gif'
width='15' height='15' border='0'>";<br>
}</td>
</tr>
?
怎么执行?Javascript代码都没有<Script>标签.
eval("h"+i+"img")绝对可以执行
document.all("h"+i+"img")应该也可以,效率可能稍低,
不过对Javascript这样的语言应该无所谓
应该是
for(img=1;img<2;img++)吧
你没有h0img其他地方的代码也有同样的问题
修改你的代码后已经完全通过
for(img=1;img<2;img++){
eval("h"+i+"img").innerHTML="<img src='images/tree_folder3.gif' width='15' height='15' border='0'>";
}
能不能把你改的代碼帖出來給我?