vue動態新增input(背景色相異)
index單/雙套用不同class
by cactus77kiki
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<div id="app">
<h2>Todos:</h2>
<button v-on:click="btnPush" type="button">新增
</button>
{{todos}}
<ol>
<li v-for="(todo,index) in todos" v-bind:class="{'odd': index % 2 === 0, 'even': index % 2 !== 0 }">
<label>
{{index}}
<input type="checkbox"
v-on:change="toggle(todo)"
v-bind:checked="todo.done">
<del v-if="todo.done">
{{ todo.text }}
</del>
<span v-else>
{{ todo.text }}
</span>
<input type="text" v-model="todo.cname"/>
</label>
</li>
</ol>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
.odd{
background: #ffff66;
}
.even {
background: #99ccff;
}
JavaScript
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript", done: false,cname:"" },
{ text: "Learn Vue", done: false ,cname:""},
{ text: "Play around in JSFiddle", done: true ,cname:""},
{ text: "Build something awesome", done: true ,cname:""}
]
},
methods: {
toggle: function(todo){
todo.done = !todo.done
},
btnPush:function(){
//alert("gggg");
var newone = {};//new Object();
newone.text = "";
newone.done=false;
newone.cname="";
this.todos.push(newone); //this.todos.push({text:"aaaa",done:false,cname:"ggg"});
}
}
})