1
我使用insertRow
到TR元素插入表中,但由於某些原因,當我做nextSibling它並不指望該插入TR元素作爲其他的兄弟姐妹。nextSibling問題與表
例如,請考慮下面的表格。
<table id="myTable">
<tr id="row_0">
<td colspan="3">Row 1</td>
</tr>
<tr id="tr_0_0">
<td>Option 1</td>
<td>Option2</td>
<td>Option 3</td>
</tr>
<tr id="tr_0_1">
<td>Option 1</td>
<td>Option 2</td>
<td>Option 3</td>
</tr>
</table>
所以之後我這樣做:
var lTable = document.getElementById("myTable");
var trEle = lTable.insertRow(-1);
trEle.id = "tr_0_2";
var cell1 = trEle.insertCell(0);
cell1.innerHTML = "Option 1";
var cell2 = trEle.insertCell(1);
cell2.innerHTML = "Option 2";
var cell3 = trEle.insertCell(-1);
cell3.innerHTML = "Option 3";
然後我用這個辦法把所有的兄弟姐妹,但它從來沒有讓我在這裏的最後一個兄弟,但只有當它已經通過添加的insertRow,因爲它可以讓所有nextSiblings就好了,但一旦我通過添加的insertRow兄弟姐妹它從來沒有給我,去年的兄弟姐妹,argggg ....
var tempTr = document.getElementById("row_0");
var totalSibs = 0;
while(tempTr.nextSibling != null)
{
var tempId = tempTr.id;
// If no id, not an element, or not a tr_0 id.
if (!tempId || tempTr.nodeType != 1 || tempId.indexOf("tr_0") != 0)
{
tempTr = tempTr.nextSibling;
continue;
}
// This NEVER ALERTS the last id of the row that was inserted using insertRow, arggg.
alert(tempId);
totalSibs++;
tempTr = tempTr.nextSibling;
}
所以,totalSibs
應該返回3貝科使用後,我插入的行,應該有3個兄弟姐妹,而是返回2,從不計數3兄弟.... arggg。
是否有人可以幫助我在這裏?
OMG,你是天才,謝謝! :) – SoLoGHoST 2010-05-25 05:15:33