源码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>事件修饰符</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
<style>
/*让所有的元素距离上面元素的距离为 20px*/
*{
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>
<!-- 1. -->
<!-- 点击后,会调整到网址: softool.cn-->
<a href="https://www.softool.cn" @click="showInfo">点我提示信息 会跳转到href指定的网址</a> <br/>
<!-- 为了不让点击后跳转,那么就需要使用 .prevent,即:阻止默认事件(跳转) -->
<a href="https://www.softool.cn" @click.prevent="showInfo">点我提示信息 不会跳转到href指定的网址</a>
<!-- 2. 阻止事件冒泡(常用) -->
<!-- 点击的时候,触发第1次弹窗 -->
<div class="demo1" @click="showInfo">
<!-- 触发第2次弹窗,但是被 .stop 阻止了 -->
<button @click.stop="showInfo">点我提示信息</button>
<!-- 修饰符可以连续写 -->
<!-- <a href="http://www.atguigu.com" @click.prevent.stop="showInfo">点我提示信息</a> -->
</div>
<!-- 3. 事件只触发一次(常用) -->
<button @click.once="showInfo">点我提示信息(仅弹窗1次)</button>
<!-- 4. 使用事件的捕获模式 -->
<div class="box1" @click.capture="showMsg(1)">
div1
<!-- 点击 box2 的时候,
默认的执行顺序是:先事件捕获,再事件冒泡(才去处理事件)
如果想先先执行冒泡事件,那么就可以把 .capture 加在外面的 box1 身上(就表示在 box1 的捕获阶段就开始处理了)。
-->
<div class="box2" @click="showMsg(2)">
div2
</div>
</div>
<!-- 5. 只有event.target是当前操作的元素时才触发事件; -->
<div class="demo1" @click.self="showInfo">
<button @click="showInfo">点我提示信息</button>
</div>
<!-- 6.事件的默认行为立即执行,无需等待事件回调执行完毕; -->
<!-- .scroll 是给滚动条加的滚动事件。滚动条滚动到最下面的时候,不会再继续往下滚动 -->
<!-- <ul @wheel.passive="demo" class="list"> -->
<!-- .wheel 是鼠标滚轮滚动的时候触发的事件。 滚轮滚动到最下面的时候,依然会继续往下滚动
.wheel 执行 demo() 的for()循环完毕之后,才会去执行滚动条的滚动操作。如果再加上 .passive 就会在执行for()同时即可执行滚动条的滚动操作了。
-->
<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">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
new Vue({
el:'#root',
data:{
name:'ST'
},
methods:{
showInfo(e){
alert('同学你好!')
// console.log(e.target)
},
showMsg(msg){
console.log(msg)
},
demo(){
for (let i = 0; i < 100000; i++) {
console.log('#') //输出一个 # 号
}
console.log('累坏了')
}
}
})
</script>
</html>