關(guān)于添加行函數(shù)的一點小疑問
var?num=2; ??????????function?addtr(){ ?????????? ????????????num++; ????????????var?a=document.getElementById("table"); ????????????var?hang=document.createElement("tr"); ????????????var?lie1=document.createElement("td"); ????????????var?lie2=document.createElement("td"); ????????????var?del=document.createElement("td"); ???????????? ????????????lie1.innerHTML="xh00"+num; ????????????lie2.innerHTML="第"+num+"個學(xué)生"; ????????????del.innerHTML="<a?href='javascript:;'?onclick='del(this)'?>刪除</a>" ???????? ???????? ?????????????a.appendChild(hang); ????????????hang.appendChild(lie1); ????????????hang.appendChild(lie2); ????????????hang.appendChild(del); ???????? ?????}
看了例示的答案,在定義添加行函數(shù)的時候是這樣的一部分??墒菫槭裁催@里獲取table中的信息的時候用的是document.getElementById而不是用的getElementsByTagName呢?table不應(yīng)該是一個標(biāo)簽名嗎?
我在主體結(jié)構(gòu)Body中也沒有發(fā)現(xiàn)有id是table的字段..但是如果我改成ByTagName來獲取table結(jié)果卻是運行不出來的,請問這是為什么呢?
2016-05-07
?<body>
?? ??? <table border="1" width="50%" id="table">
?? ??? <tr>
?? ??? ?<th>學(xué)號</th>
?? ??? ?<th>姓名</th>
?? ??? ?<th>操作</th>
?? ??? </tr>?
默認(rèn)已經(jīng)設(shè)置了ID的
2016-04-15
ByTagName獲取的得到是個數(shù)組 你可以在后面加個[0]獲取