Blogger Information
Blog 10
fans 0
comment 0
visits 4951
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
可编辑的表格
人生如梦
Original
603 people have browsed it

换了种方式,勉强算实现了

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. td input{
  8. font-size: 16px;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <table border="1">
  14. <tr>
  15. <th width="100">学科</th>
  16. <th width="400">说明</th>
  17. </tr>
  18. <tr>
  19. <td>Java</td>
  20. <td>何以解忧,唯有Java</td>
  21. </tr>
  22. <tr>
  23. <td>Python</td>
  24. <td>人生苦短,要学Python</td>
  25. </tr>
  26. <tr>
  27. <td>PHP</td>
  28. <td>PHP是世界上最好的编程语言</td>
  29. </tr>
  30. </table>
  31. <script>
  32. tdObj = document.getElementsByTagName('td');
  33. var content = '';
  34. for (let i in tdObj) {
  35. tdObj[i].onclick = function (e) {
  36. content = this.innerText;
  37. var inputObj = document.createElement('input');
  38. inputObj.name = 'temp'; //好像不加name也没什么问题
  39. inputObj.value = content;
  40. inputObj.style.width = '100%';
  41. inputObj.style.height = '100%';
  42. inputObj.style.border = 'none';
  43. inputObj.style.outline = 'none';
  44. this.innerHTML = '';
  45. this.append(inputObj);
  46. inputObj.focus();
  47. inputObj.onclick = function (e) {
  48. e.stopPropagation(); //防止input冒泡
  49. }
  50. inputObj.oninput = function (e) {
  51. content = inputObj.value; //更新临时内容
  52. }
  53. inputObj.onblur = function (e) {
  54. this.parentNode.innerHTML = content; //更改父节点内容
  55. }
  56. inputObj.onkeydown = function (e) {
  57. if (e.code === 'Enter') { //想用keyCode == 13 貌似不建议用了
  58. this.parentNode.innerHTML = content;
  59. }
  60. }
  61. }
  62. }
  63. </script>
  64. </body>
  65. </html>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:不加name没问题是因为你是使用getElementsByTagName('td'),依据是标签名,是一个临时的标签,如果后续需要表单提交之类的则一定要设置,Esc的功能可以添加一下
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post