<!DOCTYPE html> <html lang="en"> <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"> <title>Document</title> </head> <body> <div> <input type="text" name="" id="txt-title"/> <button id="btn-submit">submit</button> </div> <div> <ul id="ul-list"></ul> </div> <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <script> var $txtTitle = $('#txt-title'); var $ulList = $('#ul-list'); var $btnSubmit = $('#btn-submit'); $btnSubmit.click(function(){ var title = $txtTitle.val(); var $li = $('<li>'+ title +'</li>') $ulList.append($li); $txtTitle.val(''); }) </script> </body> </html>
這是jquery實現一個todolist的過程,經過點擊事件,初始化一個新的li標籤,append進去的。css
<!DOCTYPE html> <html lang="en"> <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"> <title>Document</title> </head> <body> <div id="app"> <input v-model='title'/> <button v-on:click='add'>submit</button> <ul v-for='item in list'> {{item}} </ul> </div> <script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script> <script> var data = { title: '', list: [] } var vm = new Vue({ el: '#app', data: data, methods: { add: function(){ this.list.push(this.title); this.title = '' } } }) </script> </body> </html>
這是vue實現todolist的過程。無論裏面的變量仍是函數都跟vue對象裏面有對應關係。在點擊按鈕的時候只須要操做裏面的數據就好了。html