【问题标题】:Get <tr> items form a Table从表中获取 <tr> 项
【发布时间】:2017-08-16 07:15:05
【问题描述】:

我有这张桌子:

<table class="table table-hover">
            <thead>
              <tr>
                <th>No.</th>
                <th>Name</th>
                <th>Type</th>
                <th>Phone</th>
                <th>S.N.</th>
                <th>Date</th>
              </tr>
            </thead>
            <tbody>
              <tr style="cursor:pointer" onclick="mandaDetalle(this.id)">
                <td>PE-6</td>
                <td>John Smith</td>
                <td>GT5</td>
                <td>845254585</td>
                <td>56456456456</td>
                <td>14/07/2017</td>
              </tr>
            </tbody>
          </table>

如何将带有项目的 &lt;tr&gt; 元素的 onclick 发送到 javascript(或 jquery)函数中,我已经发送了那个 &lt;tr&gt; 的 ID,但我还需要发送项目,因为我需要操作它们,我需要将每个&lt;th&gt; 存储到一个变量中,以便以后使用它们。

【问题讨论】:

标签: javascript html


【解决方案1】:

你可以这样做:

(1) 将id 属性添加到您的行

(2) 将onclick 事件侦听器附加到之前设置的id 找到的行

(3)查看event.target获取点击的表格单元格

(简体)HTML:

<table>
  <tr id="row">
    <td>
      first cell
    </td>
    <td>
      second cell
    </td>
  </tr>
</table>

JavaScript

function onRowClick(event) {
  console.log(event.target);
}

document.getElementById('row').addEventListener('click', onRowClick);

Here's the JSFiddle for a demo.(确保打开您的网络控制台)

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多