1 v-on起步練習(xí)

1、v-on起步練習(xí)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>V-on起步</title>
        <!-- 通過(guò)CDN引入Vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <!-- Vue-app的根容器 -->
        <div id="app">
            <button type="button" @click="handleClick">點(diǎn)我</button>
        </div>
        <script type="text/javascript">
            // 實(shí)例化一個(gè)Vue對(duì)象
            var app=new Vue({
                el:'#app',
                data:{
                    name: '軟件1721',
                },
                methods: {
                    handleClick:function(){
                        alert(this.name);
                    }
                }
            })
        </script>
    </body>
</html>

運(yùn)行結(jié)果:

image

2、V-on練習(xí)2-隱藏和顯示的切換練習(xí)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>V-on練習(xí)2-隱藏和顯示的切換練習(xí)</title>
        <!-- 通過(guò)CDN引入Vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <!-- Vue-app的根容器 -->
        <div id="app">
            <h2 v-if="show">{{name}}</h2>
            <button type="button" @click="handleClick">隱藏/顯示</button>

        </div>
        <script type="text/javascript">
            // 實(shí)例化一個(gè)Vue對(duì)象
            var app=new Vue({
                el:'#app',
                data:{
                    show:true,
                    name: '軟件1721',
                },
                methods: {
                    handleClick:function(){
                        // 把當(dāng)前show屬性的值取反
                        /* if (this.show === true) {
                            this.show=false;
                        } else{
                            this.show=true;
                        } */
                        this.show=!this.show
                    }
                }
            })
        </script>
    </body>
</html>

運(yùn)行結(jié)果:

image

3、V-on練習(xí)3-年齡的加減

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>V-on練習(xí)3-年齡的加減</title>
        <!-- 通過(guò)CDN引入Vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <!-- Vue-app的根容器 -->
        <div id="app">
            <h2> {{age}}</h2>
            <button type="button" @click="add">加一歲</button>
            <button type="button" @click="substract(5)">減五歲</button>
        </div>
        <script type="text/javascript">
            // 實(shí)例化一個(gè)Vue對(duì)象
            var app=new Vue({
                el:'#app',
                data:{
                    age:30
                },
                methods: {
                    add: function(){
                        this.age += 1;
                    },
                    substract: function(num){
                        // 判斷當(dāng)前年齡是否夠減
                        if (this.age -num<0) {
                            alert('不夠減了')
                        } else{
                            this.age -= num;
                        }

                    }
                }
            })
        </script>
    </body>
</html>

運(yùn)行結(jié)果:

image

4、V-on練習(xí)4-結(jié)合圖標(biāo)關(guān)注和取關(guān)的練習(xí)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>V-on練習(xí)4-結(jié)合圖標(biāo)關(guān)注和取關(guān)的練習(xí)</title>
        <!-- 通過(guò)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"/>
    </head>
    <style type="text/css">
        .followed{
            color: darkgrey;
        }
        .link{
            cursor: pointer;
        }
        .cancle-followed{
            color: #008000;
        }
    </style>
    <body>
        <!-- Vue-app的根容器 -->
        <div id="app">
            <h2>{{name}}</h2>
            <span class="followed link" v-show="followed" @click="handleFollow">
                  <i class ="icon-ok"></i>已關(guān)注
            </span>
          <span class="cancle-followed link" v-show="followed === false" @click="handleFollow">
              <i class ="icon-plus"></i>關(guān)注
          </span>

        </div>
        <script type="text/javascript">
            // 實(shí)例化一個(gè)Vue對(duì)象
            var app=new Vue({
                el:'#app',
                data: {
                    name: '簡(jiǎn)書(shū)作者',
                    followed:false
                },
                methods: {
                    handleFollow: function(){
                      this.followed=!this.followed;
                    }

                    }

            })
        </script>
    </body>
</html>

運(yùn)行結(jié)果:

image
  • 總結(jié):methods屬性, 這個(gè)名字是固定的,它是一個(gè)對(duì)象,用于存儲(chǔ)各種方法。{{方法名()}}就可以調(diào)用相應(yīng)的方法
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容