javascript 单击时将 td 更改为文本框,并在用户完成后将文本框更改回 td
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31642278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing td to textbox when clicked and changing textbox back to td when user is done
提问by Dr. Banana
I'm looking for a way the user can change a td from table by clicking on it. My current setup is when a user clicks on a td, it should replace the clicked td by a textbox which contains the text from the clicked td. Then when the user clicks outside of it, or presses enter. It should change the textbox back to td.
我正在寻找一种方法,用户可以通过单击它来更改表中的 td。我当前的设置是当用户点击一个 td 时,它应该用一个文本框替换点击的 td,该文本框包含来自点击的 td 的文本。然后当用户在它外面点击,或者按下回车键。它应该将文本框更改回 td。
This is the Table I have:
这是我的表:
<table class="table" id="tableCompleted">
<thead>
<tr>
<th>Name</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
</tr>
<tr>
<td>Jesse</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>David</td>
<td>No</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
This is the script I have written, the problem I have is that, when a users clicks on a td it replaces all the td's and I havent figured out how to change it back when it loses focus. I tried using a button but that made it even worse. Also the text from the td doesn't get placed inside the textbox.
这是我编写的脚本,我遇到的问题是,当用户单击 td 时,它会替换所有 td,而我还没有想出如何在失去焦点时将其改回。我尝试使用按钮,但这使情况变得更糟。此外,来自 td 的文本不会放置在文本框内。
The script I used for it:
我用于它的脚本:
$('td').click(function () {
$('td').replaceWith(function () {
return '<input type="text" id="modify" "value="' + $('#Text').val() + '" />';
});
})
I Currently have 3 problems with it, Im not sure how to only change the td that is clicked, because all the td don't have a unique id or class. And I'm not sure how to change it back when the user is done changing it. Also It doesn't place the content of the td inside the textbox.
我目前有 3 个问题,我不知道如何只更改被点击的 td,因为所有的 td 都没有唯一的 id 或类。而且我不确定在用户完成更改后如何将其更改回来。此外,它不会将 td 的内容放在文本框中。
回答by undefined
For referring to the clicked element you can use the this
keyword:
要引用被点击的元素,您可以使用this
关键字:
$('td').on('click', function() {
$(this)...
});
Also you shouldn't replace the td
with an input
, because a tr
element can't have an input
child. Instead of it remove the textContent of the td
and append an input
to it. Here is one way of implementing this:
此外,你不应该更换td
用input
的,因为一个tr
元素不能有一个input
孩子。而不是删除 的 textContenttd
并将其附加input
到它。这是实现这一点的一种方法:
$('td').on('click', function() {
var $this = $(this);
var $input = $('<input>', {
value: $this.text(),
type: 'text',
blur: function() {
$this.text(this.value);
},
keyup: function(e) {
if (e.which === 13) $input.blur();
}
}).appendTo( $this.empty() ).focus();
});
Here is a demo using the above snippet.
You have also a more sensible option: contentEditable
property.
您还有一个更明智的选择:contentEditable
财产。
$('td').prop('contentEditable', true);
Using the above property browser handles the editing. You can either manually add a contentEditable
to the target cells in your markup or use JavaScript for modifying the attribute.
使用上述属性浏览器处理编辑。您可以手动contentEditable
向标记中的目标单元格添加 ,也可以使用 JavaScript 修改属性。
回答by fubbe
Pure javascript solution here. And additionaly jsfiddle demoFight the crime :)
纯javascript解决方案在这里。另外还有jsfiddle演示打击犯罪:)
(function(){
'use-strict';
var td = document.getElementsByTagName('td');
for(var i=0;i<td.length;i++) {
td[i].addEventListener('click', change, false);
}
function change() {
if(this.firstChild.nodeType !== 3) {
return;
}
var docFrag = document.createDocumentFragment();
var input = document.createElement('input');
input.value = this.textContent;
this.removeChild(this.firstChild);
docFrag.appendChild(input);
this.appendChild(docFrag);
}
}());