詳解Vue方法與事件
更新時間:2017年03月09日 16:25:54 作者:半畝花田
本篇文章主要介紹了詳解Vue方法與事件。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
一 vue方法實現(xiàn)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue方法與事件</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="test">
<button @click="sayHi">點擊我</button> <!--這里使用@-->
</div>
<script type="text/javascript">
var myVue = new Vue({
el: '#test',
methods: { //這里使用methods
sayHi: function () {
alert('我被點擊了')
}
}
})
</script>
</body>
</html>
二 方法傳參
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue方法與事件</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="test">
<button @click="sayHi('你好')">說你好</button> <!--這里使用@-->
<button @click="sayHi('我被點擊了')">說我被點擊了</button> <!--這里使用@-->
</div>
<script type="text/javascript">
var myVue = new Vue({
el: '#test',
methods: { //這里使用methods
sayHi: function (message) {
alert(message)
}
}
})
</script>
</body>
</html>
三 vue訪問原生 DOM 事件
注意用$event獲取
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue方法與事件</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="test">
<button @click="changeColor('你好',$event)">點擊我</button> <!--這里使用@-->
<div style="height: 100px;width: 100px;background-color: red;" @mouseover="over('鼠標從我上面滑過',$event)">
鼠標從我上面滑過試試
</div>
</div>
<script type="text/javascript">
var myVue = new Vue({
el: '#test',
methods: { //這里使用methods
changeColor: function (message, event) {
alert(message+event); //彈出我被點擊了,事件是[object MouseEvent]
},
over :function (message, event) {
alert(message+event); //彈出鼠標從我上面滑過,事件是[object MouseEvent]
}
}
})
</script>
</body>
</html>
四 事件修飾符
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue方法與事件</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="test">
<button @click.stop="sayHi('你好')">說你好</button> <!-- 阻止單擊事件冒泡 -->
<button @click.prevent="sayHi('你好')">說你好</button> <!-- 提交事件不再重載頁面 -->
<button @click.stop.prevent="sayHi('你好')">說你好</button> <!-- 阻止單擊事件冒泡和提交事件不再重載頁面 -->
<button @click.capture="sayHi('你好')">說你好</button> <!-- 添加事件偵聽器時使用 capture 模式 -->
<button @click.self="sayHi('你好')">說你好</button> <!-- 只當事件在該元素本身(而不是子元素)觸發(fā)時觸發(fā)回調(diào) -->
<div @keyup.13="sayHi('你好')">說你好</div> <!-- 只有在 keyCode 是 13 時調(diào)用 vm.submit() -->
</div>
<script type="text/javascript">
var myVue = new Vue({
el: '#test',
methods: { //這里使用methods
sayHi: function (message) {
alert(message)
}
}
})
</script>
</body>
</html>
本文下載:vue-click_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中this.$refs有值,但無法獲取ref的值問題及解決
這篇文章主要介紹了vue中this.$refs有值,但無法獲取ref的值問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
vue+elemen實現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn)
el-tooltip組件本身就是懸浮提示功能,在對它進行二次封裝時,實現(xiàn)超出的文本浮現(xiàn),本文就來介紹一下vue+elemen實現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn),感興趣的可以了解一下2023-12-12
詳解如何在 vue 項目里正確地引用 jquery 和 jquery-ui的插件
本篇文章主要介紹了詳解如何在 vue 項目里正確地引用 jquery 和 jquery-ui的插件,具有一定的參考價值,有興趣的可以了解一下2017-06-06
vue實現(xiàn)數(shù)字動態(tài)翻牌的效果(開箱即用)
這篇文章主要介紹了vue實現(xiàn)數(shù)字動態(tài)翻牌的效果(開箱即用),實現(xiàn)原理是激將1到9的數(shù)字豎直排版,通過translate移動位置顯示不同數(shù)字,本文通過實例代碼講解,需要的朋友可以參考下2019-12-12

