Vue 事件处理 -- 事件修饰符(prevent、stop、capture、self、once)

1. 事件修饰符

Vue中的事件修饰符:

  1. prevent:阻止默认事件(常用);
  2. stop:阻止事件冒泡(常用);
  3. once:事件只触发一次(常用);
  4. capture:使用事件的捕获模式;
  5. self:只有event.target是当前操作的元素时才触发事件;

示例代码:

html 复制代码
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8" />
<title>事件修饰符</title>
<!-- 引入Vue -->
<script type="text/javascript" src=".../js/vue.js"></script>
<style>
* {
margin-top: 20px;
}

	<span class="token selector">.demo1</span> <span class="token punctuation">{<!-- --></span>
		<span class="token property">height</span><span class="token punctuation">:</span> 50px<span class="token punctuation">;</span>
		<span class="token property">background-color</span><span class="token punctuation">:</span> skyblue<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token selector">.box1</span> <span class="token punctuation">{<!-- --></span>
		<span class="token property">padding</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
		<span class="token property">background-color</span><span class="token punctuation">:</span> skyblue<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token selector">.box2</span> <span class="token punctuation">{<!-- --></span>
		<span class="token property">padding</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
		<span class="token property">background-color</span><span class="token punctuation">:</span> orange<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token selector">.list</span> <span class="token punctuation">{<!-- --></span>
		<span class="token property">width</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span>
		<span class="token property">height</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span>
		<span class="token property">background-color</span><span class="token punctuation">:</span> peru<span class="token punctuation">;</span>
		<span class="token property">overflow</span><span class="token punctuation">:</span> auto<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token selector">li</span> <span class="token punctuation">{<!-- --></span>
		<span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

</head>

<body>
<!--
Vue中的事件修饰符:
1.prevent:阻止默认事件(常用);
2.stop:阻止事件冒泡(常用);
3.once:事件只触发一次(常用);
4.capture:使用事件的捕获模式;
5.self:只有event.target是当前操作的元素时才触发事件;
-->
<!-- 准备好一个容器-->
<div id="root">
<h2>欢迎来到{ {name}}学习</h2>
<!-- 阻止默认事件(常用) -->
<a href="http://www.atguigu.com" @click.prevent="showInfo">点我提示信息</a>

	<span class="token comment">&lt;!-- 阻止事件冒泡&#xff08;常用&#xff09; --&gt;</span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>demo1<span class="token punctuation">&#34;</span></span> <span class="token attr-name">&#64;click</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>showInfo<span class="token punctuation">&#34;</span></span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">&#64;click.stop</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>showInfo<span class="token punctuation">&#34;</span></span><span class="token punctuation">&gt;</span></span>点我提示信息<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
		<span class="token comment">&lt;!-- 修饰符可以连续写 先写的先起作用: 这样写就是先阻止默认行为后阻止冒泡--&gt;</span>
		<span class="token comment">&lt;!-- &lt;a href&#61;&#34;http://www.atguigu.com&#34; &#64;click.prevent.stop&#61;&#34;showInfo&#34;&gt;点我提示信息&lt;/a&gt; --&gt;</span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

	<span class="token comment">&lt;!-- 事件只触发一次&#xff08;常用&#xff09; --&gt;</span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">&#64;click.once</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>showInfo<span class="token punctuation">&#34;</span></span><span class="token punctuation">&gt;</span></span>点我提示信息<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>

	<span class="token comment">&lt;!-- 使用事件的捕获模式 --&gt;</span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>box1<span class="token punctuation">&#34;</span></span> <span class="token attr-name">&#64;click.capture</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>showMsg(1)<span class="token punctuation">&#34;</span></span><span class="token punctuation">&gt;</span></span>
		div1
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>box2<span class="token punctuation">&#34;</span></span> <span class="token attr-name">&#64;click</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>showMsg(2)<span class="token punctuation">&#34;</span></span><span class="token punctuation">&gt;</span></span>
			div2
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

	<span class="token comment">&lt;!-- 只有event.target是当前操作的元素时才触发事件&#xff1b; --&gt;</span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>demo1<span class="token punctuation">&#34;</span></span> <span class="token attr-name">&#64;click.self</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>showInfo<span class="token punctuation">&#34;</span></span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">&#64;click</span><span class="token attr-value"><span class="token punctuation attr-equals">&#61;</span><span class="token punctuation">&#34;</span>showInfo<span class="token punctuation">&#34;</span></span><span class="token punctuation">&gt;</span></span>点我提示信息<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

</body>

<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。

<span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>
	<span class="token literal-property property">el</span><span class="token operator">:</span> <span class="token string">&#39;#root&#39;</span><span class="token punctuation">,</span>
	<span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{<!-- --></span>
		<span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&#39;尚硅谷&#39;</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token literal-property property">methods</span><span class="token operator">:</span> <span class="token punctuation">{<!-- --></span>
		<span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
			<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&#39;同学你好&#xff01;&#39;</span><span class="token punctuation">)</span>
			<span class="token comment">// console.log(e.target)</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token function">showMsg</span><span class="token punctuation">(</span><span class="token parameter">msg</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
			console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</script>

</html>


1.1 prevent:阻止默认事件(常用)

html:

html 复制代码
		<!-- 阻止默认事件(常用) -->
		<a href="http://www.atguigu.com" @click.prevent="showInfo">点我提示信息</a>

js:

运行效果:

a标签的默认有跳转到href的行为,我们把默认行为禁用后,就不会跳转页面

补充:

如果不使用Vue指令,那么怎么屏蔽事件的默认行为呢?


1.2 stop:阻止事件冒泡(常用)

html:

html 复制代码
		<!-- 阻止事件冒泡(常用) -->
		<div class="demo1" @click="showInfo">
			<button @click.stop="showInfo">点我提示信息</button>
			<!-- 修饰符可以连续写 -->
			<!-- <a href="http://www.atguigu.com" @click.prevent.stop="showInfo">点我提示信息</a> -->
		</div>

js:

运行结果:

如果不组织事件冒泡:

如果不使用Vue指令,那么怎么阻止事件冒泡呢?


1.3 once:事件只触发一次(常用)

html:

html 复制代码
		<!-- 事件只触发一次(常用) -->
		<button @click.once="showInfo">点我提示信息</button>

js:

运行结果:


1.4 capture:使用事件的捕获模式

html:

html 复制代码
		<!-- 使用事件的捕获模式 -->
		<div class="box1" @click.capture="showMsg(1)">
			div1
			<div class="box2" @click="showMsg(2)">
				div2
			</div>
		</div>

js:

运行结果:

关闭事件捕获:


分析原因:


1.5 self:只有event.target是当前操作的元素时才触发事件

html:

html 复制代码
		<!-- 只有event.target是当前操作的元素时才触发事件; -->
		<div class="demo1" @click.self="showInfo">
			<button @click="showInfo">点我提示信息</button>
		</div>

js:

运行结果:

因为event.target不是当前操作的元素,所有没有触发事件,这个在一定程度上面也可以阻止冒泡

如果不加指令会怎么样?

发生了事件冒泡



相关推荐
小华同学ai2 分钟前
vue-office:Star 4.2k,款支持多种Office文件预览的Vue组件库,一站式Office文件预览方案,真心不错
前端·javascript·vue.js·开源·github·office
APP 肖提莫3 分钟前
MyBatis-Plus分页拦截器,源码的重构(重构total总数的计算逻辑)
java·前端·算法
问道飞鱼15 分钟前
【前端知识】强大的js动画组件anime.js
开发语言·前端·javascript·anime.js
k093316 分钟前
vue中proxy代理配置(测试一)
前端·javascript·vue.js
傻小胖18 分钟前
React 脚手架使用指南
前端·react.js·前端框架
程序员海军30 分钟前
2024 Nuxt3 年度生态总结
前端·nuxt.js
m0_7482567840 分钟前
SpringBoot 依赖之Spring Web
前端·spring boot·spring
web135085886351 小时前
前端node.js
前端·node.js·vim
m0_512744641 小时前
极客大挑战2024-web-wp(详细)
android·前端
若川1 小时前
Taro 源码揭秘:10. Taro 到底是怎样转换成小程序文件的?
前端·javascript·react.js