onclick="函数"用法示例

v-on:click/mouseout/mouseover/dblclick/mousedown.....

事件:

v-on:click="函数"
v-on:click/mouseout/mouseover/dblclick/mousedown.....

new Vue({ el:'#box', data:{ //数据 arr:['apple','banana','orange','pear'], json:{a:'apple',b:'banana',c:'orange'} }, methods:{ show:function(){ //方法,这里是show,不能用alert alert(1); } } });

实例:为data添加数据

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 为data添加数据</title> <style> </style> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <script> window.onload=function(){ new Vue({ el:'#box', data:{ //数据 arr:['apple','banana','orange','pear'], json:{a:'apple',b:'banana',c:'orange'} }, methods:{ add:function(){ this.arr.push('tomato');//this指代new Vue(),也是data } } }); }; </script> </head> <body> <div> <input type="button" value="按钮" v-on:dblclick="add()"> <br> <ul> <li v-for="value in arr"> {{value}} </li> </ul> </div> </body> </html>

运行效果:

onclick="函数"用法示例

实例:点击按钮,div显示/消失,切换。v-show="a"

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 点击按钮,div显示/消失,切换。v-show="a"</title> <style> </style> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <script> window.onload=function(){ new Vue({ el:'#box', data:{ //数据 a:true }, methods:{ adjust:function(){ console.log(this.a); if(this.a == true){ this.a = false; }else{ this.a = true; } } } }); }; </script> </head> <body> <div> <input type="button" value="按钮" v-on:click="adjust()"> <div v-show="a"> </div> </div> </body> </html>

实例:vue简易留言本

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> vue简易留言本</title> <style> </style> <link href="https://cdn.bootcss.com/twitter-bootstrap/2.3.2/css/bootstrap.min.css" > <script src="https://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script> <script src="https://cdn.bootcss.com/twitter-bootstrap/2.3.2/js/bootstrap.js"></script> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <script> window.onload=function(){ new Vue({ el:'#box', data:{ myData:[], username:'', name:'', age:'', nowIndex:-100 }, methods:{ add:function(){ this.myData.push({ name:this.username, age:this.age }); this.username=''; this.age=''; }, deleteMsg:function(n){ if(n==-2){ this.myData=[]; }else{ this.myData.splice(n,1); } } } }); }; </script> </head> <body> <div> <form role="form"> <div> <label for="username">用户名:</label> <input type="text" placeholder="输入用户名" v-model="username"> </div> <div> <label for="age">年 龄:</label> <input type="text" placeholder="输入年龄" v-model="age"> </div> <div> <input type="button" value="添加" v-on:click="add()"> <input type="reset" value="重置"> </div> </form> <hr> <table> <caption>用户信息表</caption> <tr> <th>序号</th> <th>名字</th> <th>年龄</th> <th>操作</th> </tr> <tr v-for="(item,index) in myData"> <td>{{index+1}}</td> <td>{{item.name}}</td> <td>{{item.age}}</td> <td> <button data-toggle="modal" data-target="#layer" v-on:click="nowIndex=$index">删除</button> </td> </tr> <tr v-show="myData.length!=0"> <td colspan="4"> <button v-on:click="nowIndex=-2" data-toggle="modal" data-target="#layer" >删除全部</button> </td> </tr> <tr v-show="myData.length==0"> <td colspan="4"> <p>暂无数据....</p> </td> </tr> </table> <!--模态框 弹出框--> <div role="dialog"> <div> <div> <div> <button type="button" data-dismiss="modal"> <span>&times;</span> </button> <h4>确认删除么?</h4> </div> <div> <button data-dismiss="modal">取消</button> <button data-dismiss="modal" v-on:click="deleteMsg(nowIndex)">确认</button> </div> </div> </div> </div> </div> </body> </html>

运行效果:

onclick="函数"用法示例

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/950052f6551a241774ae303de4035da3.html