Vue 事件修饰符
跳到导航
跳到搜索
https://www.bilibili.com/video/BV1Zy4y1K7SH?p=15
事件冒泡:html 中,节点和它的父节点绑定了相同的事件处理函数,当触发事件时会触发两次。
当点击发生,先经过的阶段是事件捕获,随后才是事件冒泡,默认情况下,事件冒泡阶段才是去处理事件的。
捕获阶段是由外往内(由父元素到子元素)的,随后再触发的是冒泡阶段,冒泡阶段是由内往外(子元素到父元素)的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>事件修饰符</title>
<script type="text/javascript" src="../js/vue.js"></script>
<style>
* {
margin-top: 20px;
}
.demo1{
height: 50px;
background-color: skyblue;
}
.box1{
padding: 5px;
background-color: skyblue;
}
.box2{
padding: 5px;
background-color: orange;
}
.list{
width: 200px;
height: 200px;
background-color: peru;
overflow: auto;
}
li {
height: 100px;
}
</style>
</head>
<body>
<!--
Vue 中的事件修饰符:
1.prevent:阻止默认事件(常用);
2.stop:阻止事件冒泡(常用);
3.once:事件只触发一次(常用)
4.capture:使用事件的捕获模式;
5.self:只有 event.target 是当前操作的元素时才出发事件;
6.passive:事件的默认行为立即执行,无需等待时间回调执行完毕;
-->
<div id="root">
<h2>欢迎来到{{name}}</h2>
<!-- 阻止默认事件(常用) -->
<a href="http://www.bing.com/" @click.prevent="showInfo">点我提示信息</a>
<!-- 阻止事件冒泡(常用) -->
<div class="demo1" @click="showInfo">
<button @click.stop="showInfo">点我提示信息</button>
</div>
<!-- 事件只触发一次(常用) -->
<button @click.once="showInfo">点我提示信息</button>
<!-- 使用事件的捕获模式 -->
<div class="box1" @click.capture="showMsg(1)">
div1
<div class="box2" @click="showMsg(2)">
div2
</div>
</div>
<!-- 只有 event.target 是当前操作的元素时才出发事件 -->
<div class="demo1" @click.self="showInfo">
<button @click="showInfo">点我提示信息</button>
</div>
<!-- 事件的默认行为立即执行,无需等待事件的回调执行完毕 -->
<ul @scroll="demo" class="list">
<!-- <ul @wheel="demo" class="list"> -->
<!-- <ul @wheel.passive="demo" class="list"> -->
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
</body>
<script type="text/javascript">
new Vue({
el:'#root',
data:{
name:'北京'
},
methods:{
showInfo(e){
//e.preventDefault();
//e.stopPropagation()
// alert('hello');
console.log(e.target);
},
showMsg(msg){
console.log(msg);
},
demo(){
// console.log('@');
for (let index = 0; index < 100000; index++) {
console.log('#');
}
console.log('累坏了');
}
}
})
</script>
</html>