当前位置:Gxlcms > 前端框架 > 单击列表修改其内容(前端部分)怎么做(代码)

单击列表修改其内容(前端部分)怎么做(代码)

时间:2021-07-01 10:21:17 帮助过:101人阅读

前端做假修改(只更改显示数据),实际更改数据(更改数据库)后台执行。

当前代码段为前端代码展示: (推荐学习:前端视频)

HTML:

test.html

  1. <!DOCTYPE html>
  2. <html class="no-js">
  3. <!--<![endif]-->
  4. <head>
  5. <script src="js/test.js"></script>
  6. <style>
  7. td {
  8. border:solid 1px;
  9. width:200px;
  10. height:auto;
  11. text-align:center;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <table>
  17. <tr>
  18. <td οnclick="test(this)">test1</td>
  19. <td οnclick="test(this)">test2</td>
  20. </tr>
  21. <tr>
  22. <td οnclick="test(this)">test1</td>
  23. <td οnclick="test(this)">test2</td>
  24. </tr>
  25. </table>
  26. </body>
  27. </html>

JS:

test.js

  1. var firstValue = "";
  2. var nowDom = "";//当前操作的td
  3. //点击更改事件
  4. function test(doms) {
  5. doms.removeAttribute("onclick");
  6. nowDom = doms;
  7. var text = doms.innerText;
  8. doms.innerHTML = '<input type="text" value="' + text + '" id="input" οnchange="chane(this)" οnblur="inputOnblur(this)"/>';
  9. firstValue = text;
  10. document.getElementById("input").focus();
  11. }
  12. //文本框更改事件
  13. function chane(doms) {
  14. var text = doms.value;
  15. if (text != firstValue) {
  16. //提交后台更改数据库
  17. //前端操作
  18. nowDom.innerHTML = ""+text;
  19. nowDom.setAttribute("onclick", "test(this)");
  20. }
  21. }
  22. //文本框失焦事件
  23. function inputOnblur(doms) {
  24. var text = doms.value;
  25. if (text != firstValue) {
  26. //提交后台更改数据库
  27. }
  28. nowDom.innerHTML = "" + text;
  29. nowDom.setAttribute("onclick", "test(this)");
  30. }

原理就是点击时将文本改成输入框即可!

效果演示:

点击文本后效果:

web-1.png

鼠标失焦或者回车保存后效果:

web-2.png

以上就是单击列表修改其内容(前端部分)怎么做(代码)的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行