【发布时间】:2019-01-12 09:53:40
【问题描述】:
我在表格中添加了 contenteditable 道具。我通过使用 keydown 事件获取数据,当用户点击“Enter”时,它会接受新的输入并模糊该区域。 但用户也可以点击输入区域以外的任何地方进行模糊。(这是可编辑的功能)。我不能听这个动作。如何为此操作创建事件侦听器,例如 keydown。
这适用于“输入”,但不能点击
<html>
<head></head>
<body>
<table id="table">
<thead>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
</tr>
</thead>
<tbody>
<tr>
<th>#1</th>
<td contenteditable="true">Editable1</td>
<td contenteditable="true">Editable2</td>
</tr>
</tbody>
</table>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
</body>
</html>
$('#table tbody tr').keydown(function (event){
enter = event.which == 13,
el = event.target;
if(enter){
el.blur();
console.log("entered");
console.log($(this).find(':nth-child(2)').html());
console.log($(this).find(':nth-child(3)').html());
}
});
【问题讨论】:
-
您能否更新您的问题并包含相关的
HTML或包含一个有效的sn-p?谢谢。 -
@NewToJS 我认为现在更清楚了。
-
只需在文档上添加一个 keydown 监听器即可获取您的表格内容。
$(document).keydown(function (event){ /* fetch your '#table tbody tr stuff */ });
标签: javascript jquery events contenteditable mouselistener