如何使用 jQuery 統計表格中的列數


要使用 jQuery 統計表格中的列數,請使用 each() 函式和 attr()。你可以執行以下程式碼來了解如何統計表格中的列

示例

線上演示

<html>
  <head>
    <title>jQuery Example</title>
    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      var num = 0;
      $('tr:nth-child(1) td').each(function () {
        if ($(this).attr('colspan')) {
          num += +$(this).attr('colspan');
        } else {
        num++;
      }
    });
   alert("Total Columns= "+num);
});
</script>
</head>
<body>
  <table>
  <tr>
    <td>-1st column-</td>
    <td colspan="1">-2nd column-</td>
    <td colspan="1">-3rd column-</td>
  <tr>
  </table>
</body>
</html

更新於: 20-6-2020

660 次檢視次數

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.