【问题标题】:How do I get the value of a table heading in relation to the td element using Jquery如何使用 Jquery 获取与 td 元素相关的表标题的值
【发布时间】:2021-11-27 02:01:58
【问题描述】:

我想要实现的目标

用户单击表格中的任何

元素,并在输出中显示初始重力 (IG)、最终重力 (FG) 和 ABV,如下所示:

初始重力:1.024 > 最终重力:0.994 = 3.9% ABV。

故障

  • 表格的左侧标题是初始重力

  • iniGrav 变量应该从表格的左侧标题中获取

    值。
  • 表格的顶部标题是最终重力

  • finGrav 变量应该从表的顶部标题中获取

    值。
  • abv 变量应该从被点击的

    本身获取值。

    我的问题

    我的行 (iniGrav) 和 abv 正常工作,但我不知道如何从表的顶部标题中获取 finGrav 值。我不知道如何找到被点击的列标题值。

    在下面的代码中,我注释掉了我正在尝试的当前 finGrav 变量,并将其替换为“我应该显示 finGrav”

    所以我当前的输出是:

    • 初始重力:1.024 > 最终重力:我应该显示 finGrav = 3.9% ABV

    而不是

    • 初始重力:1.024 > 最终重力:0.994 = 3.9% ABV。

    上面的例子假设用户点击了下图中的 3.9

    元素。

    如何从表头中找到我的最终重力值并将其提供给 finGrav 变量?

    任何帮助表示赞赏,在此先感谢。

    代码

    $('td').click(function() {
    
      // iniGrav is the Initial Gravity row of th down the left
      var iniGrav = $(this).closest("tr").find('th:eq(0)').text();
    
    
      // finGrav should be the row of headings for Final Gravity across the top
      // var finGrav = $(this).closest("table .topHead ").find('th:eq(0)').text();
      var finGrav = "I should show finGrav";
    
    
      var abv = $(this).text();
      var msg = "Initial Gravity: " + iniGrav + 
              " > Final Gravity: "  + finGrav + 
                " = " + abv + "% ABV";
    
      // var title = $(this).title= msg ;
      // alert(msg);
    
      $('#output').html(msg);
    });
    body {
      background: #111;
    }
    
    #output {
      color: #fff;
      margin: 10px auto;
    }
    
    .card-body table {
      width: 100%;
      font-size: 1.2em;
    }
    
    .blank-cell,
    .row-1 td {
      background: #000;
      color: #fff;
    }
    
    td,
    th {
      padding: 2px;
      position: relative;
      outline: 0;
    }
    
    td {
      text-align: center;
      cursor: pointer;
    }
    
    th {
      font-weight: 600;
      color: #0dead0;
    }
    
    td:hover {
      background: #530288;
      color: white;
      box-shadow: inset 0 0 6px 0 #fff;
    }
    
    td {
      font-size: 14px;
      color: #fff;
      width: 10%;
      margin: 0 auto;
      text-align: center;
      text-indent: 6px;
      margin-bottom: 1em;
      position: relative;
      height: 25px;
      border-radius: 4px;
      border: 1px #111 solid;
      -webkit-transition: 0.4s linear;
      -moz-transition: 0.4s linear;
      -ms-transition: 0.4s linear;
      -o-transition: 0.4s linear;
      transition: 0.4s linear;
      -webkit-transition-property: width, background-color;
      -moz-transition-property: width, background-color;
      -ms-transition-property: width, background-color;
      -o-transition-property: width, background-color;
      transition-property: width, background-color;
      -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.25), inset 0 1px rgba(255, 255, 255, 0.1);
      box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.25), inset 0 1px rgba(255, 255, 255, 0.1);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <script src="https://unpkg.com/boxicons@2.0.9/dist/boxicons.js"></script>
    <link href='https://unpkg.com/boxicons@2.0.9/css/boxicons.min.css' rel='stylesheet'>
    <table>
    
    
      <thead class="sticky-thead">
    
        <tr>
          <th>
            <i class='bx bxs-down-arrow' style='color:#ffffff'>IG</i>
            <i class='bx bxs-right-arrow' style='color:#ffffff'>FG</i>
          </th>
          <th scope="col" class="topHead" ">0.990</th>
          <th scope="col " class="topHead "">0.992</th>
          <th scope="col" class="topHead" ">0.994</th>
          <th scope="col " class="topHead "">0.996</th>
        </tr>
      </thead>
    
      <tbody>
        <tr>
          <th scope="row">1.020</th>
          <td class="tool-tip">3.9</td>
          <td class="tool-tip">3.7</td>
          <td class="tool-tip">3.4</td>
          <td class="tool-tip">3.2</td>
        </tr>
    
        <tr>
          <th scope="row">1.022</th>
          <td class="tool-tip">4.2</td>
          <td class="tool-tip">3.9</td>
          <td class="tool-tip">3.7</td>
          <td class="tool-tip">3.4</td>
        </tr>
    
        <tr>
          <th scope="row">1.024</th>
          <td class="tool-tip">4.5</td>
          <td class="tool-tip">4.2</td>
          <td class="tool-tip">3.9</td>
          <td class="tool-tip">3.7</td>
        </tr>
    
        <tr>
          <th scope="row">1.026</th>
          <td class="tool-tip">4.7</td>
          <td class="tool-tip">4.5</td>
          <td class="tool-tip">4.2</td>
          <td class="tool-tip">3.9</td>
        </tr>
      </tbody>
    </table>
    
    <div id="output"></div>

    【问题讨论】:

    标签: html jquery css html-table


    【解决方案1】:

    获取被点击的TD的索引,并使用它来获取标题行中的对应元素。

    $('td').click(function() {
    
      // iniGrav is the Initial Gravity row of th down the left
      var iniGrav = $(this).closest("tr").find('th:eq(0)').text();
    
      var index = $(this).index();
      // finGrav should be the row of headings for Final Gravity across the top
      var finGrav = $(this).closest("table").find("tr:first th").eq(index).text();
    
    
      var abv = $(this).text();
      var msg = "Initial Gravity: " + iniGrav +
        " > Final Gravity: " + finGrav +
        " = " + abv + "% ABV";
    
      // var title = $(this).title= msg ;
      // alert(msg);
    
      $('#output').html(msg);
    });
    body {
      background: #111;
    }
    
    #output {
      color: #fff;
      margin: 10px auto;
    }
    
    .card-body table {
      width: 100%;
      font-size: 1.2em;
    }
    
    .blank-cell,
    .row-1 td {
      background: #000;
      color: #fff;
    }
    
    td,
    th {
      padding: 2px;
      position: relative;
      outline: 0;
    }
    
    td {
      text-align: center;
      cursor: pointer;
    }
    
    th {
      font-weight: 600;
      color: #0dead0;
    }
    
    td:hover {
      background: #530288;
      color: white;
      box-shadow: inset 0 0 6px 0 #fff;
    }
    
    td {
      font-size: 14px;
      color: #fff;
      width: 10%;
      margin: 0 auto;
      text-align: center;
      text-indent: 6px;
      margin-bottom: 1em;
      position: relative;
      height: 25px;
      border-radius: 4px;
      border: 1px #111 solid;
      -webkit-transition: 0.4s linear;
      -moz-transition: 0.4s linear;
      -ms-transition: 0.4s linear;
      -o-transition: 0.4s linear;
      transition: 0.4s linear;
      -webkit-transition-property: width, background-color;
      -moz-transition-property: width, background-color;
      -ms-transition-property: width, background-color;
      -o-transition-property: width, background-color;
      transition-property: width, background-color;
      -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.25), inset 0 1px rgba(255, 255, 255, 0.1);
      box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.25), inset 0 1px rgba(255, 255, 255, 0.1);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <script src="https://unpkg.com/boxicons@2.0.9/dist/boxicons.js"></script>
    <link href='https://unpkg.com/boxicons@2.0.9/css/boxicons.min.css' rel='stylesheet'>
    <table>
    
    
      <thead class="sticky-thead">
    
        <tr>
          <th>
            <i class='bx bxs-down-arrow' style='color:#ffffff'>IG</i>
            <i class='bx bxs-right-arrow' style='color:#ffffff'>FG</i>
          </th>
          <th scope="col" class="topHead">0.990</th>
          <th scope="col" class="topHead">0.992</th>
          <th scope="col" class="topHead">0.994</th>
          <th scope="col" class="topHead">0.996</th>
        </tr>
      </thead>
    
      <tbody>
        <tr>
          <th scope="row">1.020</th>
          <td class="tool-tip">3.9</td>
          <td class="tool-tip">3.7</td>
          <td class="tool-tip">3.4</td>
          <td class="tool-tip">3.2</td>
        </tr>
    
        <tr>
          <th scope="row">1.022</th>
          <td class="tool-tip">4.2</td>
          <td class="tool-tip">3.9</td>
          <td class="tool-tip">3.7</td>
          <td class="tool-tip">3.4</td>
        </tr>
    
        <tr>
          <th scope="row">1.024</th>
          <td class="tool-tip">4.5</td>
          <td class="tool-tip">4.2</td>
          <td class="tool-tip">3.9</td>
          <td class="tool-tip">3.7</td>
        </tr>
    
        <tr>
          <th scope="row">1.026</th>
          <td class="tool-tip">4.7</td>
          <td class="tool-tip">4.5</td>
          <td class="tool-tip">4.2</td>
          <td class="tool-tip">3.9</td>
        </tr>
      </tbody>
    </table>
    
    <div id="output"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多