DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>事件的基本使用title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h2>你好{{name}}同学h2>
<button @click="showInfo1">点我(不传参)button>
<button @click="showInfo2($event, 555)">点我(传参)button>
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
const vm = new Vue({
el:'#root',
data:{
name:'Mr.Wang',
},
methods:{
showInfo1(event){
console.log('点我(不传参)方法开始')
console.log(event.target.innerText)
console.log(this === vm) //此处的this是VueModel
console.log('点我(不传参)方法结束')
},
showInfo2(event,number){
console.log('点我(传参)方法开始')
console.log(event,number)
console.log(event.target.innerText)
console.log(this) //此处的this是vm
console.log('点我(传参)方法结束')
}
}
})
script>
html>

prevent:阻止默认事件(常用)
stop:阻止事件冒泡(常用)
once:事件只触发一次(常用)
capture:使用事件的捕获模式,在捕获阶段就完成触发事件

self:只有event.target是当前操作的元素时才触发事件
passive:事件的默认行为立即执行,无需等待事件回调执行完毕
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>事件修饰符title>
<script type="text/javascript" src="../js/vue.js">script>
<style>
*{
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>
<div id="root">
<h2>欢迎{{name}}进来学习h2>
<a href="http://www.baidu.com" @click.prevent="showInfo($event, 'prevent')">点我(prevent)a>
<div class="demo1" @click="showInfo">
<button @click.stop="showInfo($event, 'stop')">点我(stop)button>
div>
<button @click.once="showInfo($event, 'once')">点我(once)button>
<div class="box1" @click.capture="showMsg(1)">
div1
<div class="box2" @click="showMsg(2)">
div2
div>
div>
<div class="demo1" @click.self="showInfo($event, 'self')">
<button @click="showInfo">点我button>
div>
<ul @wheel.passive="buzyCB" class="list">
<li>1li>
<li>2li>
<li>3li>
<li>4li>
ul>
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
new Vue({
el:'#root',
data:{
name:'Mr.Wang'
},
methods:{
showInfo(e, modifier = 'common'){
// e.preventDefault(); // 正常情况下调这个方法阻止默认行为
// e.stopPropagation(); // 正常情况下调这个方法阻止冒泡
console.log(modifier, ':',e.target)
},
showMsg(msg){
console.log(msg)
},
buzyCB(){
for (let i = 0; i < 10000; i++)
console.log('~')
console.log('累屁了都!')
}
}
})
script>
html>

DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>键盘事件title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h2>欢迎{{name}}~h2>
<input type="text" placeholder="按下回车提示输入"
@keydown.enter="showInfo($event, 'enter')"><br/>
<input type="text" placeholder="按下回车提示输入"
@keydown.huiche="showInfo($event, '自定义')" >
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
Vue.config.keyCodes.huiche = 13 //定义了一个别名按键
new Vue({
el:'#root',
data:{
name:'Mr.Wang'
},
methods: {
showInfo(e, modifier){
// if(e.keyCodes !== 13) return // 普通js写法是使用keyCode判断按键
console.log(e.key, '-', e.keyCode, '--',
modifier, '--value:', e.target.value)
}
},
})
script>
html>
