当前位置:网站首页>ES6事件绑定(v-on用法)
ES6事件绑定(v-on用法)
2022-07-29 01:47:00 【青青草原小魔王】
目录
一、v-on的基本使用
1.基本形式
两个点击按钮所执行的操作的相同的,1和2的区别在于,1的简写形式就是2。
<div id="app">
<button v-on:click="change()">点击</button> <!-- 1 -->
<button @click="change2()">点击</button> <!-- 2 -->
</div>
<script>
const vm = new Vue({
el: '#app',
data() {
return {
}
},
methods: {
change() {
console.log('1111');
},change2() {
console.log('2222');
}
}
})
</script>
2.案列展示
通过点击'+','-'按钮实行显示数字的增减。
<div id="app">
<button @click="sum++">+</button>
<div>{
{sum}}</div>
<button @click="sum--">-</button>
</div>
<script src="../js/vue.js"></script>
<script>
var vm = new Vue({
el: '#app',
data() {
return {
sum: 0,
}
},
})
</script>
二、v-on的参数传递
1.基本传参
<div id="app">
<button @click="handle(4418)">点击</button>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
},
methods: {
handle: function (p) {
console.log(p)
}
}
})点击按钮后,生成传入的值4418。

事件没传参,可以省略()。
事件调用方法传参了,写函数时候省略了小括号,但是函数本身是需要传递一个参数的,这个参数就是原生事件event参数传递进去。
2.传其他参数
<div id="app">
<button @click="handle(4418,$event)">点击</button>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
},
methods: {
handle: function () {
// console.log(p)
console.log(event.target.tagName); // 获取对象
console.log(event.target.innerHTML); //获取标签 innerHTML获取里面内容
}
}
})
</script>
三、v-on的修饰词
1.阻止冒泡事件(stop,prevent)
没有阻止冒泡事件,所有功能正常。

<div id="app">
<div>{
{num}}</div>
<div v-on:click="handle"><button v-on:click.stop="handle1">点击</button>
</div>
<a href="https://www.baidu.com/" v-on:click.prevent="handle1">百度</a>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
num: 0
},
methods: {
handle: function () {
this.num++;
event.stopPropagation();
},
handle1: function (event) {
},
}
})
</script>使用冒泡后:
2.修饰词可以串联
<div id="app">
<div @click="divClick" class="div">
<a href="http://www.baidu.com" @click.stop.prevent="change">去百度</a>
</div>
</div>
<script>
const vm = new Vue({
el: '#app',
data() {
},
methods: {
change() {
console.log('阻止了默认行为也阻止了冒泡');
},
}
})
</script>
3.其他修饰词(不常用)
添加事件监听器时使用事件捕获模式。 capture
只当在 event.target 是当前元素自身时触发处理函数。 prevent
点击事件将只会触发一次。 once
.passive 修饰符尤其能够提升移动端的性能。 .passive
四、案例——简易计算器
<div id="app">
<div>请输入第一位数:<input type="text" v-model="a"></div>
<div>请输入 运算符:<input type="text" v-model="b"></div>
<div>请输入第二位数:<input type="text" v-model="c"></div>
<button @click="handle">计算</button>
<div>计算的结果为: <span v-text="msg"></span></div>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
msg: '',
a: '',
b: '',
c: '',
},
methods: {
handle() {
if (this.b == '+') {
this.msg = parseInt(this.a) + parseInt(this.c);
} else if (this.b == '-') {
this.msg = parseInt(this.a) - parseInt(this.c);
} else if (this.b == '*') {
this.msg = parseInt(this.a) * parseInt(this.c);
} else {
this.msg = parseInt(this.a) / parseInt(this.c);
}
}
}
})
</script>
边栏推荐
- 实验二:Arduino的三色灯实验
- “两个披萨”团队的分支管理实践
- Jetpack -- navigation realizes page Jump
- 开启TLS加密的Proftpd安全FTP服务器安装指南
- NPM install reports an error: eperm: operation not permitted, rename
- Basic working principle and LTSpice simulation of 6T SRAM
- Thermistor temperature calculation formula program
- [electronic components] zener diode
- 响应式织梦模板家装建材类网站
- Motionlayout -- realize animation in visual editor
猜你喜欢

什么是作用域和作用域链

Excel 打开包含汉字的 csv 文件出现乱码该怎么办?

当我看源码的时候,我在想什么?

记一次 ERROR scheduler.AsyncEventQueue: Dropping event from queue shared导致OOM

字符流综合练习解题过程

Day 14: continued day 13 label related knowledge

Awvs cannot start problem

NPM install reports an error: eperm: operation not permitted, rename

Responsive Zhimeng template decoration design website

Prometheus + AlertManager 消息预警
随机推荐
How to customize a new tab in Duoyu security browser?
Ignore wechat font settings
响应式织梦模板家装装饰类网站
[circuit design] peak voltage and surge current
Form verification hidden input box is displayed before verification
Kubesphere-多节点安装
什么是作用域和作用域链
【MQTT从入门到提高系列 | 09】WireShark抓包分析MQTT报文
Jmeter之BeanShell生成MD5加密数据写入数据库
QT qstackedwidget multi interface switching
QT memory management tips
基于对象的实时空间音频渲染丨Dev for Dev 专栏
Custom MVC principle and framework implementation
ES6 语法扩展
多线程浅谈
关于高并发,我想聊一聊。
连PostgreSQL问题:expected authentication request from server, but received v
Type analysis of demultiplexer (demultiplexer)
Leetcode 242. valid anagram
Hexadecimal to string