VUE 方法
1、$event 变量
$event 变量用于访问原生DOM事件。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>vue示例</title>
</head>
<body>
<div id="app">
点击次数:{{counter}}
<button @click="handleAdd(1,$evert)">+1</button>
<button @click="handleAdd(10,$evert)">+10</button>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
counter: 0
},
methods: {
handleAdd: function(counter, event) {
//阻止默认事件
event.preventDefault();
counter = counter || 1;
this.counter += counter;
}
}
});
</script>
</body>
</html>
2、修饰符
<!--阻止事件冒泡-->
<a @click.stop="handle()"></a>
<!--阻止默认事件-->
<a @click.prevent="handle()"></a>
<!--修饰符可以串联-->
<a @click.prevent.stop="handle()"></a>
<!--只有事件本身(而非子元素)触发-->
<a @click.self="handle()"></a>
<!--只触发一次,组件同样适用-->
<a @click.once="handle()"></a>
<!--键盘事件-->
<a @keyup.enter="handle()"></a>
示例:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>vue示例</title>
</head>
<body>
<div id="app">
<!--阻止默认事件-->
<a @click.prevent="handle()" href="https://www.baidu.com/">https://www.baidu.com/</a>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
counter: 0
},
methods: {
handle: function() {
console.log(1111)
}
}
});
</script>
</body>
</html>
作者:孟繁贵 Email:meng010387@126.com 期待共同进步!