【问题标题】:How to addEventListener to table row and extract data attribure如何将EventListener添加到表行并提取数据属性
【发布时间】:2018-04-05 18:59:06
【问题描述】:

我有一个表,每行的数据属性为 data-id

<tr class="tableRow" data-id="{{this.id}}">
      <td>{{this.first_name}}</td>
      <td>{{this.last_name}}</td>
      <td>{{this.email}}</td>
 </tr>

我想为每一行添加一个 EventListener,然后在点击一行时提取 id。

这是我正在尝试但不起作用的方法:

const rowClick = document.querySelectorAll(".tableRow");

rowClick.addEventListener("click", function() {
  console.log("row was clicked");

  const id = rowClick.getAttribute("data-id");
  console.log(id);

});

请帮忙。

【问题讨论】:

    标签: addeventlistener custom-data-attribute


    【解决方案1】:

    您可以使用数据表插件来创建表。 https://datatables.net/ 它具有单击行并执行某些操作的内置功能。

    【讨论】:

    • 谢谢,但不是我要找的
    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 2018-09-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多