jQuery To do list 增加 删除 向上 向下功能

in jQuery with 0 comment
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>todolist</title>
    <style type="text/css">
        .list_con {
            width: 600px;
            margin: 50px auto 0;
        }

        .inputtxt {
            width: 550px;
            height: 30px;
            border: 1px solid #ccc;
            padding: 0px;
            text-indent: 10px;
        }

        .inputbtn {
            width: 40px;
            height: 32px;
            padding: 0px;
            border: 1px solid #ccc;
        }

        .list {
            margin: 0;
            padding: 0;
            list-style: none;
            margin-top: 20px;
        }

        .list li {
            height: 40px;
            line-height: 40px;
            border-bottom: 1px solid #ccc;
        }

        .list li span {
            float: left;
        }

        .list li a {
            float: right;
            text-decoration: none;
            margin: 0 10px;
        }
    </style>
    <script src="../js/jquery-3.4.1.js"></script>
    <script>
        $(function () {
            var $txt = $('#txt1');
            var $btn = $('#btn1');
            var $ul = $('#list');

            $btn.click(function () {
                var sVal = $txt.val();
                $txt.val('')

                if(sVal == ''){
                    alert('请输入计划内容');
                    return;
                }

                var $li = $('<li><span>'+ sVal +'</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down"> ↓ </a><a\n' +
                        'href="javascript:;" class="del">删除</a></li>');

                // var $del = $li.find('.del');
                // $del.click(function () {
                //     $(this).parent().remove();
                //
                // });

                $ul.append($li);
            });


            //下面的写法性能不高,而且让程序复杂
            // var $del = $('.del');
            // $del.click(function () {
            //     $(this).parent().remove();
            //
            // })

            //通过事件委托的方式来处理每个a标签的事件
            $ul.delegate('a','click',function () {
                var sHandler = $(this).prop('class');

                if (sHandler=='del')
                {
                    $(this).parent().remove();
                }

                if(sHandler=='up')
                {
                    if ($(this).parent().prev().length == 0){
                        alert('到顶了');
                        return;
                    };
                    $(this).parent().insertBefore( $(this).parent().prev() )
                }

                if(sHandler == 'down')
                {
                    if ($(this).parent().next().length == 0){
                        alert('到底了');
                        return;
                    };
                    $(this).parent().insertAfter($(this).parent().next());
                }

            })


        })

    </script>

</head>
<body>

<div class="list_con">
    <h2>To do list</h2>
    <input type="text" name="" id="txt1" class="inputtxt">
    <input type="button" name="" value="增加" id="btn1" class="inputbtn">

    <ul id="list" class="list">

        <li><span>学习html</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down"> ↓ </a><a
                href="javascript:;" class="del">删除</a></li>
        <li><span>学习css</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down"> ↓ </a><a
                href="javascript:;" class="del">删除</a></li>
        <!-- sfasdjf -->
        <li><span>学习javascript</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down">
            ↓ </a><a href="javascript:;" class="del">删除</a></li>

    </ul>

</div>
</body>
</html>
Responses