1.事情对象
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="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.事件冒泡
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="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; //这种就阻止了
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="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()"
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="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右键菜单
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <!-- // <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vue.js"></script> --> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="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>
@click.stop.prevent,相当于同时执行:
event.stopPropagation();
event.preventDefault();
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦