1.事情對(duì)象
創(chuàng)新互聯(lián)公司制作網(wǎng)站網(wǎng)頁(yè)找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁(yè)設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為成百上千服務(wù),創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷推廣服務(wù)!
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', methods:{ show:function(event){ console.log(event); //event 這個(gè)就是事件對(duì)象了 } } }); } </script></head><body> <div id="box"> <input type="button" value="按鈕" @click="show($event)"> </div></body></html>
通過(guò)show($event)把事件對(duì)象傳到方法里
2.事件冒泡
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', methods:{ show:function(){ alert(1); }, show1:function(){ alert(2); } } }); } </script></head><body> <div id="box"> <div @click="show1()"> <input type="button" value="按鈕" @click="show()"> </div> </div></body></html>
點(diǎn)擊按鈕的話他會(huì),執(zhí)行show ,show1方法,依次彈出1,2。
怎么來(lái)阻止
<1> 利用我們上面講過(guò)的event
對(duì)象: event.cancelBubble = true; //這種就阻止了
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', methods:{ show:function(event){ alert(1); event.cancelBubble = true; }, show1:function(){ alert(2); } } }); } </script></head><body> <div id="box"> <div @click="show1()"> <input type="button" value="按鈕" @click="show($event)"> </div> </div></body></html>
<2>利用vue的方法阻止冒泡:給HTML元素綁定click事件的時(shí)候,改為@click.stop="show()"
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', methods:{ show:function(event){ alert(1); //event.cancelBubble = true; }, show1:function(){ alert(2); } } }); } </script></head><body> <div id="box"> <div @click="show1()"> <input type="button" value="按鈕" @click.stop="show()"> </div> </div></body></html>
3.默認(rèn)行為
比如contextmenu右鍵菜單
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <!-- // <script src="vue.js"></script> --> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', methods:{ show:function(){ alert(1); }, show1:function(){ alert(2); } } }); } </script></head><body> <div id="box"> <input type="button" value="按鈕" @contextmenu="show()"> <input type="button" value="按鈕1" @contextmenu.prevent="show1()"> <p>//按鈕右擊點(diǎn)下去會(huì)依次出現(xiàn) 彈窗 1, 還有右擊的默認(rèn)菜單</p> <p>//按鈕1右擊只出現(xiàn) 彈窗 2</p> </div></body></html>
@click.stop.prevent,相當(dāng)于同時(shí)執(zhí)行:
event.stopPropagation();
event.preventDefault();
文章標(biāo)題:vue阻止事件
當(dāng)前網(wǎng)址:http://jinyejixie.com/article36/gceppg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)、企業(yè)建站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)