javascript - 检测 TD 可编辑更改并更改 css 颜色

标签 javascript jquery html html-table

<分区>

这看起来很简单,但我想不出解决方案。

我有一个 HTML 表格,例如

$('#result_table td').on('change', function() {
  console.log("Row Changed!")
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="result_table" class="table table-hover">
  <thead>
    <tr>
      <th>Blah</th>
      <th>Error</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>bleh</td>
      <td>False</td>
    </tr>
    <!-- If Error, make it editable but highlight row red-->
    <tr bgcolor="#FF9494">
      <td contenteditable="true">blah_error</td>
      <td contenteditable="true">True</td>
    </tr>
  </tbody>
</table>

我想要做的是,如果您单击并编辑可编辑内容,将表格行背景颜色更改为不同的颜色。我一直在尝试找出 jQuery。

最佳答案

td 上使用 blur 事件,一旦你离开单元格,它就会触发你的事件

$('#result_table td').on('blur', function() {
  console.log("Row Changed!")
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="result_table" class="table table-hover">
  <thead>
    <tr>
      <th>Blah</th>
      <th>Error</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>bleh</td>
      <td>False</td>
    </tr>
    <!-- If Error, make it editable but highlight row red-->
    <tr bgcolor="#FF9494">
      <td contenteditable="true">blah_error</td>
      <td contenteditable="true">True</td>
    </tr>
  </tbody>
</table>

关于javascript - 检测 TD 可编辑更改并更改 css 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657566/

相关文章:

javascript - 需要 JQUERY Ajax 我的多维 JSON 的帮助

javascript - 在每行内的单元格内搜索标签名称

javascript - 为什么我的 Rails 应用程序中的链接会这样做?

javascript - 按字母顺序排列的数组排序 - 小写在前

javascript - 与父 div 匹配的子 div 的高度

javascript - Javascript Ajax 会导致死锁吗?

javascript - 发布后 JQuery 发布请求失败且没有警报

javascript - 是否可以使用 .autocomplete() 的修改?

html - 可以调整透明图像的大小吗?

javascript - Vue - 是否可以动态设置 html 元素的样式?