詳解vue事件對象、冒泡、阻止默認行為
更新時間:2017年03月20日 16:03:13 作者:閣下長的好生俊俏
本篇文章主要介紹了詳解vue事件對象、冒泡、阻止默認行為,這里整理了詳細的代碼,有需要的小伙伴可以參考下。
整理文檔,搜刮出一個vue事件對象、冒泡、阻止默認行為的代碼,稍微整理精簡一下做下分享。
事件對象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="../js/Vue.js" charset="utf-8"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', data:{}, methods:{ show:function(ev){ alert(ev.clientX); alert(ev.clientY); } } }); } </script> </head> <body> <div id="box"> <input type="button" name="" value="按鈕" @click="show($event)"> </div> </body> </html>
事件冒泡
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="../js/Vue.js" charset="utf-8"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', data:{}, methods:{ show:function(){ alert(111); //原生的寫法 //ev.cancelBubble = true; }, show2:function(){ alert(222); } } }); } </script> </head> <body> <div id="box"> <div @click="show2()"> <input type="button" name="" value="按鈕" @click.stop="show()"> </div> </div> </body> </html>
阻止事件默認行為
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> .show2{ padding:15px;; } </style> <script src="../js/Vue.js" charset="utf-8"></script> <script type="text/javascript"> window.onload = function () { var vm = new Vue({ el: '#box', data: {}, methods: { show: function () { alert(111) }, show2: function () { alert(222) } } }); } </script> </head> <body> <div id="box"> <div class="show2"> <input type="button" name="" value="按鈕" @contextmenu.prevent="show()"> </div> </div> </body> </html>
希望本文所述對你有所幫助,vue事件對象、冒泡、阻止默認行為內容就給大家介紹到這里了。希望大家繼續(xù)關注我們的網站!想要學習vue可以繼續(xù)關注本站。
相關文章
vue在響應頭response中獲取自定義headers操作
這篇文章主要介紹了vue在響應頭response中獲取自定義headers操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07