【问题标题】:HTML eventlistener on each row of table表格每一行的 HTML 事件监听器
【发布时间】:2020-11-13 07:32:07
【问题描述】:

问题来了:

  • 我有一个表,其中的行包含按键侦听器
  • 每一行都是 contentEditable
  • 我想让包装的 tbody 元素内容可编辑(以允许通过单击和拖动来选择多行)
  • 如果我确实使 tbody contentEditable 按键事件不再 在行中触发
    • 只有 tbody 元素接收事件

主要问题是将 tbody 设置为可编辑会掩盖表格行的按键事件

编辑: 是否有任何形式的指针事件:键盘事件没有?

如何让行仍然接收事件,或者如何在不使 tbody/table 内容可编辑的情况下启用拖动和选择?

非常感谢任何反馈或帮助,谢谢!


如果描述不清楚,这里是一个示例代码 sn-p:

<style>
    table{
      background-color: cyan;
    }
    table, th,td, tr{
      border: 1px solid black;
      border-collapse: collapse;
    }
    
    tr,td, th {
      padding: 3px;
      text-align: left;
      height: 2em;
    }
    
</style>

<div>
<table>
  <thead style = "background-color:white">
    <tr>
      <th>Value</th>
    </tr>
   </thead>
   <tbody> <!-- need to make this tbody contenteditable -->
    <tr contenteditable = 'true' onkeypress='keyPressed(event)'>
        <td>v1</td>
    </tr>
    <tr contenteditable = 'true' onkeypress='keyPressed(event)'>
        <td>v2</td>
    </tr>
   </tbody>
</table>
</div>

<script>
    function keyPressed(){
        if(event.keyCode == 13){
          event.preventDefault();
          row = event.target;
          row.insertAdjacentHTML('afterend',"<tr contenteditable = 'true' onkeypress='keyPressed(event)'><td></td></tr>");
          row.nextSibling.focus();
        }
    }
</script>

【问题讨论】:

  • 这能回答你的问题吗? How to addEventListener to table cells
  • 为此使用表格似乎是错误的做法,为什么不将它们作为可以拖动的简单 div 呢?
  • @imvain2 我认为你是对的,div 是更好的方法。

标签: javascript html css


【解决方案1】:
document.querySelectorAll('contenteditable td')
.forEach(e => e.addEventListener("click", function() {
    // Here, `this` refers to the element the event was hooked on
    console.log("clicked")
}));

lear more.

【讨论】:

  • 这不起作用,如果总体 tbody 设置为 contenteditable,它仍然会掩盖行按键事件。
【解决方案2】:

您可以使用element.target 查看实际点击的是哪个元素。例如,在我的 sn-p tbodytr 中具有 contenteditable 但正如您在控制台中看到的那样,element.targetelement.currentTarget 都检索不同的字段,因此您知道可以这样使用它.

但我可能只推荐使用 div,因为它们更容易使用。

document.querySelector('[contenteditable]').addEventListener("click", function(el) {
   console.log(el.target,el.currentTarget)
});
/*
    function keyPressed() {
      if (event.keyCode == 13) {
        event.preventDefault();
        row = event.target;
        row.insertAdjacentHTML('afterend', "<tr contenteditable = 'true' onkeypress='keyPressed(event)'><td></td></tr>");
        row.nextSibling.focus();
      }
    }*/
table {
  background-color: cyan;
}

table,
th,
td,
tr {
  border: 1px solid black;
  border-collapse: collapse;
}

tr,
td,
th {
  padding: 3px;
  text-align: left;
  height: 2em;
}
<div>
  <table>
    <thead style="background-color:white">
      <tr>
        <th>Value</th>
      </tr>
    </thead>
    <tbody contenteditable='true'>
      <!-- need to make this tbody contenteditable -->
      <tr contenteditable='true'>
        <td>v1</td>
      </tr>
      <tr contenteditable='true'>
        <td>v2</td>
      </tr>
    </tbody>
  </table>
</div>

【讨论】:

  • 此解决方案不适用于将事件类型切换为“keypress”,tbody 既是 currentTarget 又是目标(有趣的是,与 click 事件不同)。我很可能会切换到 div,但是,为了知道,我很想弄清楚这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2018-03-18
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多