阻止事件冒泡
作者:互联网
0. 使用.stop和.prevent
1. 示例1:
阻止事件冒泡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.c1{
background-color: tan;
height: 200px;
}
.c2{
background-color: red;
height: 100px;
width: 100px;
}
</style>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<div class="c1" @click="f1">
<!-- 阻止事件冒泡 -->
<!-- <div class="c2" @click.stop="f2"></div>-->
<div class="c2" @click.stop.prevent="f2"></div>
</div>
<!-- 阻止标签默认动作 -->
<!-- <a href="http://www.baidu.com" @click.stop.prevent="">百度</a>-->
</div>
</body>
<script>
var card = new Vue({
el:"#app",
data:{
username:'chao'
},
methods:{
f1(){
alert('负极标签');
},
f2(){
alert('子标签')
}
},
});
</script>
</html>
2. 综合案例-todolist
我的计划列表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>todolist</title>
<style type="text/css">
.list_con{
width:600px;
margin:50px auto 0;
}
.inputtxt{
width:550px;
height:30px;
border:1px solid #ccc;
padding:0px;
text-indent:10px;
}
.inputbtn{
width:40px;
height:32px;
padding:0px;
border:1px solid #ccc;
}
.list{
margin:0;
padding:0;
list-style:none;
margin-top:20px;
}
.list li{
height:40px;
line-height:40px;
border-bottom:1px solid #ccc;
}
.list li span{
float:left;
}
.list li a{
float:right;
text-decoration:none;
margin:0 10px;
}
</style>
<script src="js/vue.js"></script>
</head>
<body>
<div id="todolist" class="list_con">
<h2>To do list</h2>
<input type="text" v-model="message" class="inputtxt">
<input type="button" @click="addItem" value="增加" class="inputbtn">
<ul id="list" class="list">
<li v-for="item,key in dolist">
<span>{{item}}</span>
<a @click="upItem(key)" class="up" > ↑ </a>
<a @click="downItem(key)" class="down"> ↓ </a>
<a @click="delItem(key)" class="del">删除</a>
</li>
</ul>
</div>
<script>
// 计划列表代码
let vm = new Vue({
el:"#todolist",
data:{
message:"",
dolist:[
"学习html",
"学习css",
"学习javascript",
]
},
methods:{
addItem(){
if(this.messsage==""){
return false;
}
this.dolist.push(this.message);
this.message = ""
},
delItem(key){
this.dolist.splice(key, 1);
},
upItem(key){
if(key==0){
return false;
}
// 向上移动
let result = this.dolist.splice(key,1); // 注意返回的是数组
this.dolist.splice(key-1,0,result[0]);
},
downItem(key){
// 向下移动
let result = this.dolist.splice(key, 1);
console.log(result);
this.dolist.splice(key+1,0,result[0]);
}
}
})
</script>
</body>
</html>
标签:result,splice,dolist,list,height,阻止,冒泡,key,事件 来源: https://blog.csdn.net/qq_52385631/article/details/123286324