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


本文摘自PHP中文网,作者(*-*)浩,侵删。

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

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

HTML:

test.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<!DOCTYPE html>

  

<html class="no-js">

<!--<![endif]-->

<head>

    <script src="js/test.js"></script>

<style>

    td {

        border:solid 1px;

        width:200px;

        height:auto;

        text-align:center;

    }

  

</style>

</head>

<body>

    <table>

        <tr>

            <td οnclick="test(this)">test1</td>

            <td οnclick="test(this)">test2</td>

        </tr>

        <tr>

            <td οnclick="test(this)">test1</td>

            <td οnclick="test(this)">test2</td>

        </tr>

    </table>

</body>

</html>

JS:

test.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

var firstValue = "";

var nowDom = "";//当前操作的td

//点击更改事件

function test(doms) {

    doms.removeAttribute("onclick");

    nowDom = doms;

    var text = doms.innerText;

    doms.innerHTML = '<input type="text" value="' + text + '" id="input"  οnchange="chane(this)"  οnblur="inputOnblur(this)"/>';

    firstValue = text;

    document.getElementById("input").focus();

}

  

//文本框更改事件

function chane(doms) {

    var text = doms.value;

    if (text != firstValue) {

        //提交后台更改数据库

  

        //前端操作

        nowDom.innerHTML = ""+text;

        nowDom.setAttribute("onclick", "test(this)");

    }

}

  

//文本框失焦事件

function inputOnblur(doms) {

    var text = doms.value;

    if (text != firstValue) {

        //提交后台更改数据库

    }

    nowDom.innerHTML = "" + text;

    nowDom.setAttribute("onclick", "test(this)");

}

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

效果演示:

点击文本后效果:

web-1.png

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

web-2.png

以上就是单击列表修改其内容(前端部分)怎么做(代码)的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

轶事:chrome小恐龙背后你不知道的故事

什么是前端

storage event如何实现页面间通信

介绍 javascript 代码加不加分号有什么区别

web前端js是什么

前端工程师学习基础知识

10个你可能不知道的很棒的js字符串技巧

值得收藏的css性能优化方法

50个javascript面试和解答(收藏)

html5新增结构:html主体结构和非主体结构的介绍

更多相关阅读请进入《前端》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...