前端vue实现增删列表&原生JS实现页面的增删【代码可copy运行】

一、 vue实现

1-1 页面

在这里插入图片描述

1-2 代码展示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>增删列表的完整例子</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="todo-list-example">
        <form action="" v-on:submit.prevent="addNewTodo">
            <label for="new-todo">Add a todo</label>
            <input type="text" v-model="newTodoText" id="new-todo" placeholder="Feed the cat">
            <button>Add</button>
        </form>
        <ul>
            <todo-item v-for="(todo, index) in todos" :key="todo.id" :title="todo.title" @remove="todos.splice(index, 1)"></todo-item>
        </ul>
    </div>
    <script>
        const app = Vue.createApp({
            data() {
                return {
                    newTodoText: 'Feed the cat',
                    todos: [{
                        id: 1,
                        title: 'Do the dishes'
                    }, {
                        id: 2,
                        title: 'Do the dishes'
                    }, {
                        id: 3,
                        title: 'Do the dishes'
                    }],
                    nextTodoId: 4

                }
            },
            methods: {
                addNewTodo() {
                    this.todos.push({
                        id: this.nextTodoId++,
                        title: this.newTodoText
                    })
                    this.newTodoText = ''
                }
            }
        })
        app.component('todo-item', {
            template: `
          <li>
            {{ title }}
            <button @click="$emit('remove')">Remove</button>
          </li>
          `,
            props: ['title'],
            emits: ['remove']
        })
        app.mount('#todo-list-example')
    </script>
</body>

</html>

二、 JS实现增删列表

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>待办事项列表</title>
    <style>
        /* 事项列表样式 */
        
        ul {
            margin: 0;
            padding: 0;
            margin-top: 20px;
        }
        
        ul li {
            cursor: pointer;
            position: relative;
            padding: 12px 8px 12px 40px;
            background: #eee;
            font-size: 18px;
            transition: 0.2s;
        }
        
        ul li.checked {
            background-color: #888;
            color: #fff;
            text-decoration: line-through;
        }
        
        ul li.checked::before {
            content: '';
            position: absolute;
            border-color: #fff;
            border-style: solid;
            border-width: 0 2px 2px 0;
            top: 10px;
            left: 16px;
            transform: rotate(45deg);
            height: 15px;
            width: 7px;
        }
        
        ul li:nth-child(odd) {
            background: #f9f9f9;
        }
        /* 点击关闭按钮 */
        
        .close {
            position: absolute;
            right: 0;
            top: 0;
            padding: 12px 16px 12px 16px;
        }
        
        .close:hover {
            background-color: #f44336;
            color: white;
        }
    </style>
</head>

<body>
    <h2>添加待办事项</h2>
    <input type="text" id="myInput" placeholder="Title...">
    <button onclick="newElement()">添加</button>
    <ul id="myUL">
        <li>Hit the gym</li>
        <li class="checked">Pay bills</li>
        <li>Meet George</li>
        <li>Buy eggs</li>
        <li>Read a book</li>
        <li>Organize office</li>
    </ul>

    <script>
        // Create a "close" button and append it to each list item
        var myNodelist = document.getElementsByTagName("LI");
        var i;
        for (i = 0; i < myNodelist.length; i++) {
            var span = document.createElement("SPAN");
            var txt = document.createTextNode("\u00D7");
            span.className = "close";
            span.appendChild(txt);
            myNodelist[i].appendChild(span);
        }

        // Click on a close button to hide the current list item
        var close = document.getElementsByClassName("close");
        var i;
        for (i = 0; i < close.length; i++) {
            close[i].onclick = function() {
                var div = this.parentElement;
                div.style.display = "none";
            }
        }

        // Add a "checked" symbol when clicking on a list item
        var list = document.querySelector('ul');
        list.addEventListener('click', function(ev) {
            if (ev.target.tagName === 'LI') {
                ev.target.classList.toggle('checked');
            }
        }, false);

        // Create a new list item when clicking on the "Add" button
        function newElement() {
            var li = document.createElement("li");
            var inputValue = document.getElementById("myInput").value;
            var t = document.createTextNode(inputValue);
            li.appendChild(t);
            if (inputValue === '') {
                alert("You must write something!");
            } else {
                document.getElementById("myUL").appendChild(li);
            }
            document.getElementById("myInput").value = "";

            var span = document.createElement("SPAN");
            var txt = document.createTextNode("\u00D7");
            span.className = "close";
            span.appendChild(txt);
            li.appendChild(span);

            for (i = 0; i < close.length; i++) {
                close[i].onclick = function() {
                    var div = this.parentElement;
                    div.style.display = "none";
                }
            }
        }
    </script>
</body>

</html>

手动狗头,JS粘贴官网。vue纯手撸

  • 好看的都不是我的,哈哈哈哈
  • 代码均可运行
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值