10事件处理
# 事件处理
# 事件修饰符
修饰符3个:
event,stop,self
//stop 阻止事件的传播
<div @click="fn($event)">
内容2
<div @click.stop="fn($event)">内容1</div>
</div>
new Vue({
el: "#app",
data: {
},
methods: {
fn(e){
console.log(e.currentTarget)
}
},
});
//prevent 阻止事件的默认行为
<form>
<input type="text">
<button @click.prevent="submit">提交</button>
</form>
new Vue({
el: "#app",
data: {
},
methods: {
submit(){
console.log(123)
}
},
});
//capture 把冒泡改为捕获
<div @click.capture="fn($event)">
内容2
<div @click.capture="fn($event)">内容1</div>
</div>
new Vue({
el: "#app",
data: {
},
methods: {
fn(e){
console.log(e.currentTarget)
}
},
});
//self 当currentTarget和target都是同一个时候才触发
<div @click.self="fn($event)">
内容2
<div @click.self="fn($event)">内容1</div>
</div>
new Vue({
el: "#app",
data: {
},
methods: {
fn(e){
console.log(e.currentTarget)
console.log(e.target)
}
},
});
//once 只触发一次
<div @click.once="fn($event)">
内容
</div>
new Vue({
el: "#app",
data: {
},
methods: {
fn(e){
console.log(e.currentTarget)
}
},
});
//passive 主动告诉浏览器不需要阻止事件的默认行为
//移动端 滑动屏幕 会发询问给浏览器 需不需要阻止这个事件
//passive 不需要问我,每次都不阻止。目的是提升浏览器的运行效率
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 按键修饰符
<input type="text" @keydown.q="fn">
new Vue({
el: "#app",
data: {
},
methods: {
fn(){
console.log(123)
}
},
});
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 练习
%accordion% 仅参考💢 %accordion%
<div id="app">
<button @click="handlerClick($event)">按钮</button>
</div>
<script>
new Vue({
el: "#app",
data: {},
methods: {
handlerClick(e) {
console.log(e);
}
}
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div id="app">
<form>
<input type="text">
<button @click="submit($event)">提交</button>
</form>
</div>
<script>
new Vue({
el: "#app",
data: {},
methods: {
submit(e) {
e.preventDefault();//阻止事件的默认行为
console.log(123);
}
}
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%/accordion%
%accordion% 练习🚀 %accordion%
<div id="app">
<form>
<input type="text">
<!-- <button @click.stop="submit()">按钮</button> -->
<button @click.prevent="submit()">按钮</button>
</form>
</div>
<script>
new Vue({
el: "#app",
data: {
},
methods: {
submit() {
console.log(123);
}
}
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
%/accordion%
%accordion% 点击事件练习🔨 %accordion%
添加了
.self
<style>
.a {
width: 500px;
height: 500px;
background: red;
}
.b {
width: 300px;
height: 300px;
background: #f0f;
}
</style>
<div id="app">
<div class="a" @click.self="handler($event)">
<div class="b" @click.self="handler($event)"></div>
</div>
</div>
<script>
new Vue({
el: "#app",
data: {
},
methods: {
handler(e) {
// console.log(e.currentTarget) //冒泡事件 冒泡上去了就先b然后a
console.log(e.target) //触发该元素,添加.self点击谁就是谁
}
},
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
传播事件
<style>
.a {
width: 500px;
height: 500px;
background: red;
}
.b {
width: 300px;
height: 300px;
background: #f0f;
}
</style>
<div id="app">
<div class="a" @click="handler($event)">
<div class="b" @click="handler($event)"></div>
</div>
</div>
<script>
new Vue({
el: "#app",
data: {
},
methods: {
handler(e) {
// console.log(e.currentTarget) //冒泡事件 冒泡上去了就先b然后a
console.log(e.target) //触发该元素,触发b就两个b咯,触发a就a咯
}
},
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41