【问题标题】:Contenteditable deletes and edit the icon in field tooContenteditable 也可以删除和编辑字段中的图标
【发布时间】:2026-02-08 03:25:02
【问题描述】:

我在我的表中使用 contenteditable,并且使用 Javascript 创建的每个 td 都设置为 contenteditable="true"

问题是我想在显示“总计”的字段中添加一个图标,并允许用户更改数据,但使用 contenteditable 甚至允许他们编辑/删除图标!

我希望图标保留在那里而不是编辑或更改。有没有办法使用 JavaScript 或 CSS 修复可编辑 td 中的跨度或图标?

例子:

<table>
<thead>
  <tr>
    <th>TOTAL</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td contenteditable="true"><span>$</span>887</td>
  </tr>
</tbody>

【问题讨论】:

  • 您可以更新您的示例以显示图标,因为您抱怨在您的示例中无法重现的问题。
  • 在示例中,我使用了 $。示例中以 $ 为图标,因为 广泛用于图标。
  • 您能否将 $ 设为图标的 td,并使其部分不可编辑?
  • Zack 我试过这样做,但看起来不太好。尽管我使用的是没有单元格或网格的表格,但表格数据看起来是分开的并且彼此相距很远。

标签: javascript html css html-table contenteditable


【解决方案1】:

将 contenteditable 移动到另一个不包含图像的 span:

<table>
<thead>
  <tr>
    <th>TOTAL</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>$<span contenteditable="true">887</span></td>
  </tr>
</tbody>

【讨论】: