如何使 HTML 表格单元格可编辑?
我想让 html 表格的某些单元格可编辑,只需双击单元格,输入一些文本,所做的更改就可以发送到服务器。我不想使用一些像dojo数据网格这样的工具包。因为它提供了一些其他功能。您能为我提供一些代码片段或有关如何实现它的建议吗?
I'd like to make some cells of html table editable, simply double click a cell, input some text and the changes can be sent to server. I don't want to use some toolkits like dojo data grid. Because it provides some other features. Would you provide me some code snippet or advices on how to implement it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以在相关单元格、行或表格上使用 contenteditable 属性。
更新了 IE8 兼容性
请注意,如果您使表格可编辑,至少在 Mozilla 中,您可以删除行等。
您还需要检查目标受众的浏览器是否支持此属性。
至于侦听更改(以便您可以发送到服务器),请参阅 contenteditable 更改事件
You can use the contenteditable attribute on the cells, rows, or table in question.
Updated for IE8 compatibility
Just note that if you make the table editable, in Mozilla at least, you can delete rows, etc.
You'd also need to check whether your target audience's browsers supported this attribute.
As far as listening for the changes (so you can send to the server), see contenteditable change events
HTML5 支持 contenteditable、
第三方编辑
引用 contenteditable 上的 mdn 条目< /a>
HTML5 supports contenteditable,
3rd party edit
To quote the mdn entry on contenteditable
我有三种方法,
在这里,您可以根据您的要求使用
或
1.在
中使用输入。
在所有
中使用
元素,
此外,您可能想要将输入的大小调整为其
td
的大小。例如,您还可以在未编辑时更改输入框边框的颜色。
2.使用
contenteditable='true'
属性。 (HTML5)但是,如果您想要使用
contenteditable='true'
,您可能还需要将适当的值保存到数据库中。您可以使用 ajax 来实现这一点。您可以将按键处理程序
keyup
、keydown
、keypress
等附加到。另外,当用户连续键入,ajax 事件不会随着用户按下的每个键而触发。例如,
3。单击时将
附加到
。 >被点击,根据
td
的值替换它的值。当输入模糊时,用输入的值更改 td 的值。这一切都通过 JavaScript 完成。I have three approaches,
Here you can use both
<input>
or<textarea>
as per your requirements.1. Use Input in
<td>
.Using
<input>
element in all<td>
s,Also, you might want to resize the input to the size of its
td
. ex.,You can additionally change the colour of the border of the input box when it is not being edited.
2. Use
contenteditable='true'
attribute. (HTML5)However, if you want to use
contenteditable='true'
, you might also want to save the appropriate values to the database. You can achieve this with ajax.You can attach keyhandlers
keyup
,keydown
,keypress
etc to the<td>
. Also, it is good to use some delay() with those events when user continuously types, the ajax event won't fire with every key user press. for example,3. Append
<input>
to<td>
when it is clicked.Add the input element in
td
when the<td>
is clicked, replace its value according to thetd
's value. When the input is blurred, change the `td's value with the input's value. All this with javascript.这是一个可运行的示例。
This is a runnable example.
我将其用于可编辑字段
I am using this for editable field
您可以使用 x-editable https://vitalets.github.io/x-editable/
来自 bootstrap 的很棒的库
You can use x-editable https://vitalets.github.io/x-editable/
its awesome library from bootstrap
只需在单击单元格时动态地将
元素插入
中。只有简单的 HTML 和 Javascript。不需要
contentEditable
、jquery
、HTML5
https://Jsfiddle.net/gsivanov/38tLqobw/2/
Just insert
<input>
element in<td>
dynamically, on cell click. Only simple HTML and Javascript. No need forcontentEditable
,jquery
,HTML5
https://Jsfiddle.net/gsivanov/38tLqobw/2/
试试这个代码。
您还可以访问此链接了解更多详细信息:
Try this code.
You can also visit this link for more details :
这是要点,尽管您不需要使代码如此混乱。相反,您可以迭代所有
并添加带有属性的
,最后输入值。
This is the essential point although you don't need to make the code this messy. Instead you could just iterate through all the
<td>
and add the<input>
with the attributes and finally put in the values.如果你使用Jquery,这个插件可以帮助你
很简单,但是很好
https://github.com/samuelsantosdev/TableEdit
If you use Jquery, this plugin help you
is simple, but is good
https://github.com/samuelsantosdev/TableEdit
要设置单元格可编辑,您可以使用此关键字“contenteditable”
这是一个例子
FOR SET A CELL EDITABLE YOU CAN USE THIS KEYWORD "contenteditable"
THIS IS AN EXAMPLE
这实际上很简单,
这是我的 HTML、jQuery 示例..它的工作原理就像一个魅力,我使用在线 json 数据示例构建所有代码。
干杯
<< HTML>>
<< jQuery>>
this is actually so straight forward,
this is my HTML, jQuery sample.. and it works like a charm, I build all the code using an online json data sample.
cheers
<< HTML >>
<< jQuery >>
单击时将
添加到
中。
当
模糊时,将其更改为
。
Add
<input>
to<td>
when it is clicked.Change
<input>
to<span>
when it is blurred.如果你的表有很多功能(排序、导出、更新、编辑等),
我会推荐引导表。
关于编辑实践:
监听事件:click-cell.bs.table 然后添加属性 contenteditable 点击后立即添加到 td
您可能不希望允许在每一列中进行编辑,因此我添加了
data-editable code> 属性我自己用JS来确定这一点。
演示
Title
列允许您编辑If your table has many functions (sorting, exporting, updating, editing, etc.),
I would recommend the bootstrap table.
About editing practices:
Listen to events: click-cell.bs.table then adds the attribute contenteditable to the td as soon as it is clicked
You may not want to allow editing in every column, so I added the
data-editable
attribute myself with JS to determine this.Demo
column of
Title
allows you to edit