javascript - 索引表行 js/jquery

标签 javascript jquery html

我一直在尝试为我的表行建立索引,但没有成功。我能做的最好的事情就是提出代码,每次我添加新的表行时,它都可以重新索引表中的每一行,但它不起作用。我认为语法有问题,但我不确定是什么。 JS部分:

$("#pridet").click(function(){
  $("table tbody tr").first().clone().prependTo("table tbody");
  var x = document.getElementsByTagName("tr");
  document.getElementsByName("tabelis").innerHTML = x.rowIndex;
});

我意识到第二部分存在 fatal error (例如指示在哪里编写索引?)

html 文件中的代码:

<button id = "pridet">pridet</button>
  <table id="myTable" class="table table-inverse">
    <thead id = "headings" class = "thead-default">
      <tr>
        <th>Tabelio Nr.</th>
        <th>Vardas</th>
        <th>Pavardė</th>
        <th>Pareigos</th>
        <th>Bazinė alga, €</th>
        <th>Valandinis atlyginimas, €</th>
        <th>Veiksmai</th>
      </tr>
    </thead>
    <tfoot class = "thead-default">
      <tr>
        <td>Vidurkis</td>
        <td></td>
        <td></td>
        <td></td>
        <td>10000</td>
        <td>17.3</td>
        <td></td>
      </tr>
    </tfoot>
    <tbody>
      <tr>
        <td class = "tabelis">1</td>
        <td>Mark</td>
        <td>Otto</td>
        <td>@mdo</td>
        <td>bla</td>
        <td><input type="button" value="Delete" onclick="deleteRow(this)"></td>
        <td>
          <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p>
          <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0" onclick="deleteRow(this)"><span class = "fa fa-trash"></span></p>
        </td>
      </tr>
      <tr>
        <td class = "tabelis">2</td>
        <td>Jacob</td>
        <td>Thornton</td>
        <td>@fat</td>
        <td>bla</td>
        <td>blum</td>
        <td>
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
        </td>
      </tr>
      <tr>
        <td class = "tabelis">3</td>
        <td>Larry</td>
        <td>the Bird</td>
        <td>@twitter</td>
        <td>bla</td>
        <td>blum</td>
        <td>
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
        </td>
      </tr>
    </tbody>
  </table>
  <script src="script.js" charset="utf-8"></script>
  <script>
    function deleteRow(r) {
    var i = r.parentNode.parentNode.rowIndex;
    document.getElementById("myTable").deleteRow(i);
  };
  </script>

我可能可以用这样的方法来计算总行数

$("#counter").text( $("table tbody tr").length );

并使用该值以某种方式将数字插入表格单元格

编辑:之前没有提到这一点,但是表格行必须添加到表格顶部(标题下方)

最佳答案

$("#pridet").click(function(){
  var new_row = $("table tbody tr").first().clone();
  $('table tbody').append(new_row);
  $('table tr:last').find('td:first').html($('table tbody tr').length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id = "pridet">pridet</button>
  <table id="myTable" class="table table-inverse" border='1'>
    <thead id = "headings" class = "thead-default">
      <tr>
        <th>Tabelio Nr.</th>
        <th>Vardas</th>
        <th>Pavardė</th>
        <th>Pareigos</th>
        <th>Bazinė alga, €</th>
        <th>Valandinis atlyginimas, €</th>
        <th>Veiksmai</th>
      </tr>
    </thead>
    <tfoot class = "thead-default">
      <tr>
        <td>Vidurkis</td>
        <td></td>
        <td></td>
        <td></td>
        <td>10000</td>
        <td>17.3</td>
        <td></td>
      </tr>
    </tfoot>
    <tbody>
      <tr>
        <td class = "tabelis">1</td>
        <td>Mark</td>
        <td>Otto</td>
        <td>@mdo</td>
        <td>bla</td>
        <td><input type="button" value="Delete" onclick="deleteRow(this)"></td>
        <td>
          <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p>
          <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0" onclick="deleteRow(this)"><span class = "fa fa-trash"></span></p>
        </td>
      </tr>
      <tr>
        <td class = "tabelis">2</td>
        <td>Jacob</td>
        <td>Thornton</td>
        <td>@fat</td>
        <td>bla</td>
        <td>blum</td>
        <td>
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
        </td>
      </tr>
      <tr>
        <td class = "tabelis">3</td>
        <td>Larry</td>
        <td>the Bird</td>
        <td>@twitter</td>
        <td>bla</td>
        <td>blum</td>
        <td>
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
          <!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
        </td>
      </tr>
    </tbody>
  </table>

关于javascript - 索引表行 js/jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391768/

相关文章:

html - 父有序列表中的嵌套无序列表

jquery - 获取最后可见的 div 的属性

javascript - Web 浏览器何时请求动态创建的 iframe 的 src 内容?

javascript - 是否有可能创建一个独立的 Angularjs 应用程序,它不会与同一页面上的任何其他 Js 或 css 库发生冲突

javascript - html javascript 更改我的 <div> 的值,但刷新页面 1 秒,然后全部为 null

jquery - 为什么 jQuery ui 选项卡不起作用?

javascript - 将命令式转变为声明式 - 字符限制

javascript - 无法理解为什么它给我 : Uncaught (in promise) TypeError: Cannot read property 'blabla' of undefined

javascript - 在导航菜单中突出显示当前页面?

javascript - 使用 javascript 设置动画时,div 内边框半径不起作用