下面的这种写法:可以动态的添加标签,而且特别的简单,只不过是用vue.js 写的,如果看不懂,baidu一下vue.js
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="app">
<input v-model="newTodo" v-on:keyup.enter="clearTodo">
<ul>
<li v-for='todo in todos'>
<span>{{todo.text}}</span>
<button v-on:click="removeTodo($index)">x</button>
</li>
<button v-on:click="addTodo">+</button>
</ul>
</div>
<script>
new Vue({
el:'#app',
data:{
newTodo:'this is my world',
todos:[
{text:'this is 1 world'},
]
},
methods:{
clearTodo:function(){
this.newTodo='asdfasdfadfadfa'
},
removeTodo:function(index){
this.todos.splice(index,1)
},
addTodo:function(){
var ab=this.todos.length;
if(ab){
var cd=this.todos[ab-1].text.substr(8,2);
}else{
var cd=0;
}
console.log(cd);
this.todos.push({text:'this is '+(++cd)+' world'})
}
}
})
</script>
</body>
</html>