您现在的位置是:首页 > 电脑技术查询 > web开发

自动分配id用getElementById找不到?解决思路

编辑:chaxungu时间:2022-10-02 23:20:30分类:web开发

自动分配id用getElementById找不到?
<script language = "javascript ">
with(document)
{
write( " <table> ");
write( " <tr> ");

for(var a = 0; a <2;a ++)
{
write( " <td id =\ "calcell\ " > ");
write( " <font id =\ "celltext\ " Victor =\ "eric\ "> ");
write(a);
write( " </font> ");
write( " </td> ");
}

write( " </tr> ");
write( " </table> ");

write( " <input type =\ "button\ " value =\ "button\ " id =\ "button1\ "onclick =\ "aa();\ "> ");
}


function aa()
{
var s =100;
for(var i =0;i < 2;i++)
{

celltext[i].Victor = s++;
<!-- alert(celltext[i].Victor); -->



alert(document.getElementById(celltext[i]).Victor);

}

}

</script>


------解决方案--------------------
alert(document.getElementById( "celltext ").Victor);

------解决方案--------------------
你最好 write( " <font id = 'celltext " + a + " ' Victor =\ "eric\ "> ");

alert(document.getElementById( "celltext " +a ).Victor);
------解决方案--------------------
不可能,肯定可以啊
------解决方案--------------------
你的alert(celltext[i].Victor); 输出的是 100多,你怎么可能有这个ID的对象啊
------解决方案--------------------
ID重名了呀。
------解决方案--------------------
问题是用document.write写的,重id的元素连uniqueID都是一样的。