1. 程式人生 > >5.vue事件修飾符

5.vue事件修飾符

1.js檔案:
    //在使用vue之前必須例項化vue物件
new Vue({
    /*el:指element  需要獲取的元素,一定是html中根容器元素
        以後所有的操作均是在這個根容器中進行操作
    */
    el:"#vue-app",
    /*
    data:用於資料的儲存,用來寫屬性
        以key-value的值進行儲存
    */
    data:{
       age:30,
       x:0,
       y:0,
    },
    /**
     * 使用method來儲存方法,
     * 使用greet來進行呼叫
     * 使用this獲取當前容器的內容
    * this.age:就是指先找到當前容器的data然後在找到name對應的屬性
     */
    methods: {
        updateXY: function (event) {
           this.x=event.offsetX;
            this.y = event.offsetY;
        },
        alert: function () {
        alert("aaaaaaaaaa");
        }
    }
})
2.html檔案:
    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>index.html</title>
    <!-- 用於線上引用vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- 引用css檔案 -->
    <link rel="stylesheet" href="../css/style.css">
    <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
</head>
<body>
   
    <!--vue-app就是我們的根容器,vue均是對vue操作-->
    <div id="vue-app">
    <div  v-on:mousemove="updateXY">
        {{x}},{{y}}
        <!--滑鼠放在此處就不會被識別-->
        <span v-on:mousemove.stop="">stopMoving</span>
    </div>
    <!--注:滑鼠的點選事件是click-->
    <!--先觸發點選事件在跳轉-->
    <a @click="alert()" href="https://www.google.com/images/branding/googlelogo/2x/googlelogo_light_color_272x92dp.png" >
    aaaaa
    </a><br/>
    <!--先觸發點選事件,但是由於有prevent所以他不會在跳轉-->
    <a @click.prevent="alert()" href="https://www.google.com/images/branding/googlelogo/2x/googlelogo_light_color_272x92dp.png">
        bbbbbbbbb
    </a>
    </div>
    <script src="../js/app.js"></script>
</body>
</html>