2013-02-27 213 views
-1

由於其中有另一個標籤,因此在將div行中的行添加到表中時會出現一些問題。添加元素防止代碼向表中添加新行

我知道如果我可以更改表的ID,但因爲它生成了,我不能改變它們。我只能改變封裝表格的div的id。

<!--Adding does not work on this table --> 
<h3>Wednesday</h3> 
<button type="button" onclick="displayResult('wed')">Insert new row</button> 
<div id="wed"> 
<strong>Even this stops it from adding</strong> <!--This is the problem --> 
<table id="myTable" border="1"> 
    <tr> 
    <td>cell 1</td> 
    <td>cell 2</td> 
    </tr> 
</table> 
<div> 

<!--Adding works on this table --> 
<h3>Thursday</h3> 
<button type="button" onclick="displayResult('thu')">Insert new row</button> 
<div id="thu"> 
<table id="myTable" border="1"> 
    <tr> 
    <td>cell 1</td> 
    <td>cell 2</td> 
    </tr> 
</table> 
<div> 

function displayResult(number) { 
    var elemList=document.getElementById(number).childNodes; 
    var table=elemList[1]; 
    var row=table.insertRow(-1); 
    var cell1=row.insertCell(0); 
    var cell2=row.insertCell(1); 
    cell1.innerHTML="New"; 
    cell2.innerHTML="New"; 
} 

Here is jsFiddle illustrating the problem I have

+0

混淆的縮寫器背後是什麼?請充實你的問題。 – 2013-02-27 01:28:37

+0

在這裏看到一些會很高興,但鏈接中的小提琴已經足夠清晰,出了什麼問題。 – krillgar 2013-02-27 01:29:08

+0

@krillgar,[我們喜歡一個包含所有內容的問題](http://meta.stackexchange.com/questions/149890/prevent-posts-with-links-to-jsfiddle-and-no-code),這樣例如,如果鏈接不好,問題及其答案對未來的訪問者仍然有用。 – 2013-02-27 01:30:20

回答

1

我會通過索引元素都假設得到的是什麼問題(沒有看到生成的表的代碼,很難肯定的說),並建議,一旦你有div,你getElementsByTagName('table')[0],看看你不能從那裏工作。

我也建議,你正在做的大部分工作是更容易與jQuery,如果你有能力將其添加到項目,我會推薦它。