微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vue2.0的contextmenu右键弹出菜单的实例代码

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

1.事情对象

rush:xhtml;"> <Meta charset="utf-8">

<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#Box',methods:{
show:function(event){
console.log(event); //event 这个就是事件对象了
}
}
});
}

Box">

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

2.事件冒泡

rush:xhtml;"> <Meta charset="utf-8">

<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#Box',methods:{
show:function(){
alert(1);
},show1:function(){
alert(2);
}
}
});
}

Box">

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

怎么来阻止

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

rush:xhtml;"> <Meta charset="utf-8">

<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);
}
}
});
}

Box">

<2>利用vue的方法阻止冒泡:给HTML元素绑定click事件的时候,改为@click.stop="show()"

rush:xhtml;"> <Meta charset="utf-8">

<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);
}
}
});
}

Box">

3.认行为

比如contextmenu右键菜单

rush:xhtml;"> <Meta charset="utf-8">

<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#Box',show1:function(){
alert(2);
}
}
});
}

Box"> <p>//按钮右击点下去会依次出现 弹窗 1, 还有右击的<a href="https://www.jb51.cc/tag/mo/" target="_blank" class="keywords">默</a>认<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></p> <p>//按钮1右击只出现 弹窗 2</p>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

原文地址:https://www.jb51.cc/vue/37616.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐