美文网首页
11.Vue.js方法

11.Vue.js方法

作者: 王杰磊 | 来源:发表于2019-03-09 10:46 被阅读0次

v-on指令事件

  • v-on:click(鼠标单击事件)
  • v-on:dblclick(鼠标双击事件)
  • v-on:focusin(获得焦点事件)
  • v-on:focusout(失去焦点事件)
  • v-on:mousedown(鼠标任意一个键按下去的事件)
  • v-on:mouseup(鼠标任意一个键弹起来的事件)

示例

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>关注和取消关注练习</title>
        <!-- 通过CDN引入Vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"/>
        <style type="text/css">
            .followed{
                color: #ddd;
            }
            .cancel-followed{
                color: green;
            }
            .link{
                cursor: pointer;
            }
            .btn{
                width: 100px;
                height: 35px;
                color: rgb(234,11,90);
                border-radius: 20px;
                font-size: 14px;
                background-color: #fff;
                border: 1px solid rgb(234,11,90);
                outline: none;
                cursor: pointer;
            }
            .btn1{
                width: 100px;
                height: 35px;
                color: rgb(234,11,90);
                border-radius: 20px;
                font-size: 14px;
                background-color:orangered;
                border: 1px solid rgb(234,11,90);
                outline: none;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <!-- vueapp的根容器 -->
        <div id="app">
            <h2>{{name}}</h2>
            <span class="followed link" v-show="followed" @click="handlefollow"><i class="icon-ok"></i>已关注</span>
            <span class="cancel-followed link" v-show="followed===false" @click="handlefollow"><i class="icon-plus"></i>关注</span>
            <button type="button" class="btn" v-show="like" @click="iffollow"><i class="icon-heart-empty"></i>喜欢&nbsp;&nbsp;|{{likes}}</button>
            <button type="button" class="btn1" v-show="like===false" @click="unfollow(1)"><i class="icon-heart"></i>喜欢&nbsp;&nbsp;|{{likes}}</button>
        </div>
        <script type="text/javascript">
            // 实例化一个vue对象
            var app = new Vue({
                el: '#app',
                data: {
                    name:'简书作者',
                    followed:false,
                    likes:1000,
                    like:true
                },
                methods:{
                    handlefollow:function(){
                        this.followed=!this.followed;
                    },
                    iffollow:function(){
                        this.like=!this.like;
                        if(like=true){
                        this.likes+=1;
                        }
                    },
                    unfollow:function(num){
                        this.like=!this.like;
                        this.likes-=num;
                    }
                }
            })
        </script>
    </body>
</html>

在methods下面放方法,注意每个方法后面加“,”最后一个不用加,v-on:click可以简写成@click="方法名"可以加在任意一个元素后边

相关文章

网友评论

      本文标题:11.Vue.js方法

      本文链接:https://www.haomeiwen.com/subject/pyfspqtx.html