zoukankan      html  css  js  c++  java
  • Vue.js03:v-model实现简易计算器

    v-model用于数据的双向绑定。bug不少,凑合看吧,主要是练习v-model。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <!-- v-model 双向绑定数据 -->
            <input type="text" v-model="n1">
    
            <select v-model="opt">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
    
            <input type="text" v-model="n2">
    
            <!-- @ 用于事件绑定 -->
            <input type="button" value="=" @click="cal">
    
            <input type="text" v-model="result">
        </div>
    </body>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                n1: 0,
                n2: 0,
                result: 0,
                opt: '+'
            },
            methods: {
                cal(){
                    switch(this.opt){
                        case '+':
                            this.result = parseInt(this.n1) + parseInt(this.n2)
                        break
                        case '-':
                            this.result = parseInt(this.n1) - parseInt(this.n2)
                        break
                        case '*':
                            this.result = parseInt(this.n1) * parseInt(this.n2)
                        break
                        case '/':
                            this.result = parseInt(this.n1) / parseInt(this.n2)
                        break
                    }
                }
            }
        })
    </script>
    </html>
  • 相关阅读:
    延时显示(类QQ头像显示)
    getComputedStyle与currentStyle获取样式(style/class)
    私单
    厂里
    009 vue的响应式方法
    008 vue的v-for的使用及key的使用
    007 v-if,v-else-if,v-else和v-show
    006 Vue的v-on
    005 Vue 计算属性
    004 Vue的v-bind属性
  • 原文地址:https://www.cnblogs.com/zrmw/p/10522833.html
Copyright © 2011-2022 走看看