vue基础——事件修饰符

    科技2022-07-11  81

    事件修饰符(阻止冒泡和默认行为)

    .stop阻止了冒泡行为 <button v-on:click.stop="handle1">点击</button>

    传统的阻止冒泡:event.stopPropagation();

    .prevent阻止默认行为(阻止a标签跳转) <a href="http://www.baidu.com" v-on:click.prevent="handle2">阻止默认行为(跳转)</a>

    传统的阻止默认行为:event.preventDefault();

    案例验证:

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="vue.js"></script> </head> <body> <div id="app"> <div >{{sum}}</div> <div v-on:click="handle"> <!--阻止了冒泡--> <button v-on:click.stop="handle1">点击</button> </div> <a href="http://www.baidu.com" v-on:click.prevent="handle2">阻止默认行为(跳转)</a> </div> <script type="text/javascript"> var vm = new Vue({ el:"#app", data:{ sum:0 }, methods:{ handle:function(){ this.sum++; }, handle1:function(event){ console.log(1); // event.stopPropagation();传统的阻止冒泡 }, handle2:function(event){ // 传统的阻止默认行为 // event.preventDefault(); } } }) </script> </body> </html>
    Processed: 0.022, SQL: 8