整理文档,搜刮出一个vue2.0的contextmenu右键弹出菜单的实例代码

整理文档,搜刮出一个vue2.0的contextmenu右键弹出菜单的实例代码,稍微整理精简一下做下分享。 

1.事情对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!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  这个就是事件对象了
           }
         }
       });
     }
   </ script >
</ head >
< body >
   < div id = "box" >
     < input type = "button" value = "按钮" @ click = "show($event)" >
   </ div >
</ body >
</ html >

通过show($event)把事件对象传到方法里

2.事件冒泡

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!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 >

点击按钮的话他会,执行show ,show1方法,依次弹出1,2。

怎么来阻止

<1> 利用我们上面讲过的event对象:  event.cancelBubble = true;   //这种就阻止了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!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事件的时候,改为@click.stop="show()"

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!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.默认行为

比如contextmenu右键菜单

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!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 >//按钮右击点下去会依次出现 弹窗 1, 还有右击的默认菜单</ p >
     < p >//按钮1右击只出现 弹窗 2</ p >
   </ div >
</ body >
</ html >
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值