insertRow()到特定的行id

时间:2013-01-22 17:39:35

标签: javascript jquery dom dynamic innerhtml

使用

var row = table.insertRow(id);

如何指定新行在使用的所选ID下,而不是硬编码索引或表的末尾?我有一个下拉列表,其中包含用于放置新行的行ID的不同选项。下拉选项具有与相关行匹配的ID。谢谢。这是我的表,我希望新行去用户选择的父亲(father3,father4或father5)

 <table id="shore_tab" border="1">
                      <tr class="row_blue_bold father" id="father3">
                        <td colspan="2" class="father_header">Category 3</td>
                        <td class="cell_50">&nbsp;</td>
                      </tr>
                            <tr class="row_blue_bold son3">
                              <td class="cell_20 cell_no_bkg">&nbsp;</td>
                              <td class="cell_190">(information for father3, category 3)</td>
                              <td class="cell_50">&nbsp;</td>
                            </tr>
                            <tr class="row_blue_bold son3">
                              <td class="cell_20 cell_no_bkg">&nbsp;</td>
                              <td class="cell_190">(information for father3, category 3)</td>
                              <td class="cell_50">&nbsp;</td>
                            </tr>
                      <tr class="row_blue_bold father" id="father4">
                        <td colspan="2" class="father_header">Category 4</td>
                        <td class="cell_50">&nbsp;</td>
                      </tr>
                            <tr class="row_blue_bold son4">
                              <td class="cell_20 cell_no_bkg">&nbsp;</td>
                              <td class="cell_190">(information for father4, category 4)</td>
                              <td class="cell_50">&nbsp;</td>
                            </tr>
                            <tr class="row_blue_bold son4">
                              <td class="cell_20 cell_no_bkg">&nbsp;</td>
                              <td class="cell_190">(information for father4, category 4)</td>
                              <td class="cell_50">&nbsp;</td>
                            </tr>
                    </table>

2 个答案:

答案 0 :(得分:2)

您可以使用id获取索引

var row = table.insertRow(document.getElementById(id).rowIndex+1);//+1 to be inserted under

答案 1 :(得分:0)

id应该是index,即:table.rows就像一个数组(它实际上不是一个数组,但表现得像一个数组)。