Vue中的事件与常见的问题处理
Vue的事件:获取事件对象$event;
事件冒泡:事件会向上传播
原生js阻止事件冒泡,需要先获取事件对象,再调用stopPropagation()方法;
vue事件修饰符stop,例@clik.stop;
事件默认行为:
原生js方式需要获取事件对象,再调用preventDefault()方法;
在vue中则使用修饰符prevent,例@clik.prevent
先在button中加入获取对象$event
在控制台可以打印出该事件,可以看出target中有innerHTML

通过e.target.innerHTML,获取button标签上的名称:

vue;
<script>
window.onload= () =>{ let vm=new Vue({
el:'#two',
data:{
result:0 },
methods:{ show(e){ alert("欢迎来到perfect*博客园!!!");
console.log('欢迎来到perfect*博客园!!!');
console.log(e.target.innerHTML);
}, add(a,b){
console.log("add");
console.log(this==vm);
this.result +=a+b; }
}
})
} </script>
html:
<body>
<div id="two">
<button @click="show($event)">欢迎来到perfect*博客园 A</button>
<button @click="show($event)">欢迎来到perfect*博客园 B</button> <button @click="add(1,2)">进行绑定数据相加的方法add()</button>
result:{{result}} </div>
</body>
绑定mouseenter时可以一直触发

<button @mouseenter="add(10,20)">进行绑定数据相加的方法add()</button><br/>
result:{{result}}<br/>
当使用once时只能触发一次,之后鼠标进入时无效果:

<button @mouseenter.once="add(10,20)">进行绑定数据相加的方法add()</button><br/>
result:{{result}}<br/>
事件冒泡:
点击一个获取对象的事件按钮,会发生调用写的三个方法:

该问题的代码:
<script>
window.onload= () =>{ let vm=new Vue({
el:'#two',
data:{
result:0 },
methods:{ show(e){ console.log('欢迎来到perfect*博客园!!!');
console.log(e.target.innerHTML);
}, showA(){ console.log('欢迎来到perfect*博客园!!!A');
},
showB(){ console.log('欢迎来到perfect*博客园!!!B');
}, }
})
} </script> <body>
<div id="two"> <!--事件冒泡-->
<div @click="showA()"> <div @click="showB()">
<button @click="show($event)">欢迎来到perfect*博客园 A!!!!!!</button>
</div>
</div> </div>
</body>
解决冒泡问题的方法:
vue:在button事件中获取对象的button中的click加.stop即可;
javascript:使用e.stopPropagation();

从图中可以看出来,使用.stop时只使用了show方法
<button @click.stop="show($event)">欢迎来到perfect*博客园 A!!!!!!</button>
JavaScript代码:
show(e){
console.log('欢迎来到perfect*博客园!!!');
console.log(e.target.innerHTML);
e.stopPropagation();
}
阻止事件的默认行为
vue:使用.prevent进行阻止;
javascript:使用e.preventDefault()实现;
使用a标签作为示例,初始时可以跳转:

使用.prevent时,怎么点击都不能进行跳转:

HTML:
<!-- 阻止事件的默认行为-->
<a href="HelloVue.html" @click.prevent=showLink($event)>Click Link!!!</a>
vue:
showLink(){
console.log("已阻止链接的跳转!!");
}
使用JavaScript的写法效果同上,代码:
HTML:
<!-- 阻止事件的默认行为-->
<a href="HelloVue.html" @click=showLink($event)>Click Link!!!</a>
vue:
showLink(e){
console.log("已阻止链接的跳转!!");
e.preventDefault();
}
以上示例所有的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>event</title>
</head>
<script type="text/javascript" src="../js/vue.js" ></script>
<script>
window.onload= () =>{ let vm=new Vue({
el:'#two',
data:{
result:0 },
methods:{ // show(e){
//
//
// console.log('欢迎来到perfect*博客园!!!');
// console.log(e.target.innerHTML);
// e.stopPropagation();
// }, // add(a,b){
// console.log("add");
// console.log(this==vm);
// this.result +=a+b;
//
// }, //
// showA(){
//
// console.log('欢迎来到perfect*博客园!!!A');
// },
// showB(){
//
// console.log('欢迎来到perfect*博客园!!!B');
// }, showLink(e){
console.log("已阻止链接的跳转!!");
e.preventDefault(); } } })
} </script> <body>
<div id="two">
<!--<button @click="show($event)">欢迎来到perfect*博客园 A</button><br/>
<button @click="show($event)">欢迎来到perfect*博客园 B</button><br/> <button @click="add(1,2)">进行绑定数据相加的方法add()</button><br/>
result:{{result}}<br/> <button @mouseenter.once="add(10,20)">进行绑定数据相加的方法add()</button><br/>
result:{{result}}<br/>
--> <!--事件冒泡-->
<!--<div @click="showA()"> <div @click="showB()">
<button @click="show($event)">欢迎来到perfect*博客园 A!!!!!!</button>
</div>
</div>--> <!-- 阻止事件的默认行为-->
<a href="HelloVue.html" @click=showLink($event)>Click Link!!!</a> </div>
</body>
</html>
$event、事件冒泡、阻止事件的默认行为代码
Vue中的事件与常见的问题处理的更多相关文章
- 怎样在 Vue 中使用 事件修饰符 ?
Vue 中可以通过 v-on 来绑定事件监听函数, 不过事件会有许多额外情况, 比如 是否阻止冒泡 / 是否阻止重载 / 是否限制点击次数 / 是否可以通过按键触发 等等. 这时就需要使用到 事件修饰 ...
- 第二章 Vue快速入门--9 使用v-on指令定义Vue中的事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Vue_(基础)Vue中的事件
Vue.js中文文档 传送门 Vue@事件绑定 v-show:通过切换元素的display CSS属性实现显示隐藏: v-if:根据表达式的真假实现显示隐藏,如果隐藏,它绑定的元素都会销毁,显示的时候 ...
- Vue中键盘事件
Vue中监听 键盘事件及修饰符 键盘事件: keyCode 实际值 48到57 0 - 9 65到90 a - z ( A-Z ) 112到135 F1 - F ...
- vue中滚动事件绑定的函数无法调用问题
问题描述: 一个包含下拉加载的页面,刷新当前页然后滚动页面,能够正常触发滚动事件并调用回调函数,但是如果是进了某一个页面然后再进的该页面,滚动事件能够触发, 但是回调函数在滚动的时候只能被调用一次. ...
- (二)咋使用VUE中的事件修饰符
1,stop修饰符:阻止事件冒泡 首先我们要明确H5的事件是从内向外进行冒泡的,写一个简单的DEMO 当我们点击按钮时,事件从内向外冒泡,依次触发绑定的事件,控制台信息如下 现在我们在click后面添 ...
- vue中的事件监听之——v-on vs .$on
跟着视频中老师的教学视频学vue的时候,看很多时候都用@(v-on)来监听子级emit的自定义事件,但在bus总线那块,又用.$on来监听bus自身emit的事件,v-on之间似乎相似但又不同,今天对 ...
- vue 中scroll事件不触发问题
在vue项目中需要监听滚动条滚动的位置,结果写了scroll监听事件就是不生效,最后查资料发现是页面有样式设置了over-flow:scroll,去掉之后完美解决.(页面样式中存在over-flow: ...
- vue中的事件修饰符
vue提倡的是在方法中只有对数据的处理,所以提供了事件修饰符用于DOM的事件处理,常用的事件修饰符有以下几个: (1). stop:阻止冒泡(通俗讲就是阻止事件向上级DOM元素传递) 点击内层div的 ...
随机推荐
- 【UML】NO.48.EBook.5.UML.1.008-【UML 大战需求分析】- 组件图(Component Diagram)
1.0.0 Summary Tittle:[UML]NO.48.EBook.1.UML.1.008-[UML 大战需求分析]- 组件图(Component Diagram) Style:DesignP ...
- Stacking调参总结
1. 回归 训练了两个回归器,GBDT和Xgboost,用这两个回归器做stacking 使用之前已经调好参的训练器 gbdt_nxf = GradientBoostingRegressor(lear ...
- Jmeter安装与配置
Jmeter下载与安装配置 1.下载地址:https://jmeter.apache.org/ Apache Jmeter首页,点击 Download Releases 然后,选择,安装版本,有li ...
- 1.Spring对JDBC整合支持
1.Spring对JDBC整合支持 Spring对DAO提供哪些支持 1)Spring对DAO异常提供统一处理 2)Spring对DAO编写提供支持的抽象类 3)提高编程效率,减少DAO编码量 Spr ...
- HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)
Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...
- HDU 2604 Queuing(递推+矩阵)
Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- Semaphore wait has lasted > 600 seconds
解决方案:set global innodb_adaptive_hash_index=0;
- PM2管理工具的使用
linux上PM2可以管理服务程序,防止程序无故关闭,具有程序守护功能,自动重启服务器程序,监控程序等好处,很方便,具体自己去体会! 官网地址: http://pm2.keymetrics.io/ ...
- 使用js Math.random()函数生成n到m间的随机数字
何使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备,Math.random()函数返回0和1之间的伪随机数 摘要: 本文讲解如何使用js生成n到m间的随机数字,主要目的是为后 ...