vue中v-show和v-if在显示和隐藏元素上的区别
v-show将元素隐藏是在dom节点上加style=’display:none’
v-if是直接将元素完全去掉
拿v-show示例,(v-if 也是一样,把下面的代码中v-show替换成v-if即可运行)
<!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', data:{ isShow:false, }, methods:{ toggle:function(){ this.isShow = !this.isShow; } } }); } </script> </head> <body> <div id="box"> <input type="button" value="toggle" v-on:click="toggle()"> <br /> <div v-show="isShow" style="width: 100px;height: 100px;background: red"></div> </div> </body> </html>