如果不使用组件的话可不可以实现这个功能?​自己尝试了一下发现不知道如何获取当前点击的<li>标签的索引。

来源:3-4 实现todolist的删除功能

生若浮丶死若休_

2018-09-07 14:45

<!DOCTYPE html>

<html lang="zh-CN">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<link rel="stylesheet" href="./reset.css">

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

<title>Document</title>

</head>

<body>

<div id='root'>

<input v-model='inputValue'/>

<button @click='handleAdd'>添加</button>

<ul>

<li v-for="(item, index) in list" :key='index' :index="index" @click='handleDelete(index)'>{{item}}下标{{index}}</ul>

</ul>

</div>

</body>

<script>

new Vue({

el: '#root',

data: {

inputValue:'',

list:[]

},

methods: {

handleAdd: function() {

this.list.push(this.inputValue)

this.inputValue=''

},

handleDelete: function(e) {

this.list.splice(e.target,1)

}

}

})

</script>

</html>


写回答 关注

2回答

  • fighting加油吧
    2018-09-07 22:47:23

    你的<li>标签后面是</ul>

    delete参数直接用index就可以取到的

  • fighting加油吧
    2018-09-07 22:40:26

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset="UTF-8">

    <title>Document</title>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    </head>

    <body>

    <div id="root">

    <input type="text" v-model="inputValue">

    <button @click="inputSubmit">提交</button>

    <ul>

    <li v-for="(item,index) of list"

    @click="remove(index)">{{item}}下标{{index}}</li>

    </ul>

    </div>


    <script>

    new Vue({

    el:"#root",

    data:{

    inputValue:'',

    list:[]

    },

    methods:{

    inputSubmit:function(){

    this.list.push(this.inputValue);

    this.inputValue=""

    },

    remove:function(index){

    this.list.splice(index,1)

    }

    }

    })

    </script>

    </body>

    </html>

    你的太乱了,我自己写了一个,你看一下

vue2.5入门

快速理解Vue编程理念上手Vue2.0开发。

147227 学习 · 675 问题

查看课程

相似问题