• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

用Vue做个的比较两个数字的大小页面

武飞扬头像
每天都要做的更好
帮助1

1、 考核知识点
创建vue实例和对v-model内置指令的使用
2、 练习目标
创建vue实例
掌握v-model内置指令的使用。
3、 需求分析
初始状态下,“比较”按钮不可点击,输入一个数字,按钮仍然不可点击,当两个数字输入完后,按钮变为可点击状态;点击下方“比较”按钮,显示比较结果,大的那一边字体变大,小的那边字体变小,相等的话字体一样大:
4、 案例分析

我们用两个数字来比较:

1.初始状态下,“比较”按钮不可点击,界面如下:

学新通

2.输入一个数字,按钮仍然不可点击,当两个数字输入完后,按钮变为可点击状态:

学新通

学新通 

 3.点击下方“比较”按钮,显示比较结果,大的那一边字体变大,小的那边字体变小,相等的话字体一样大:

 学新通

学新通

 代码如下:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>input demo</title>
  6.  
    <style>
  7.  
    .compare-val {
  8.  
    color: red;
  9.  
    }
  10.  
     
  11.  
    .large {
  12.  
    font-size: 16px;
  13.  
    }
  14.  
     
  15.  
    .default {
  16.  
    font-size: 14px;
  17.  
    }
  18.  
     
  19.  
    .small {
  20.  
    font-size: 12px;
  21.  
    }
  22.  
    </style>
  23.  
    </head>
  24.  
    <body>
  25.  
    <div id="root">
  26.  
    <div>
  27.  
    <label>
  28.  
    <span>请输入第一个数: </span>
  29.  
    <input v-model.number="input1" @focus="handleFocus"/>
  30.  
    </label>
  31.  
    </div>
  32.  
    <div>
  33.  
    <label>
  34.  
    <span>请输入第二个数: </span>
  35.  
    <input v-model.number="input2" @focus="handleFocus"/>
  36.  
    </label>
  37.  
    </div>
  38.  
    <button @click="compareClick()" :disabled="input1.length === 0 || input2.length === 0">比较</button>
  39.  
    <div class="compare-val">
  40.  
    <span>比较的结果: </span>
  41.  
    <span v-show="input1 && input2 && txt">
  42.  
    <span :class="num1Size">第一个数</span><span>{{ txt }}</span><span :class="num2Size">第二个数</span>
  43.  
    </span>
  44.  
    </div>
  45.  
    </div>
  46.  
     
  47.  
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js" type="text/javascript"></script>
  48.  
    <script type="text/javascript">
  49.  
    new Vue({
  50.  
    el: "#root",
  51.  
    data() {
  52.  
    return {
  53.  
    input1: "",
  54.  
    input2: "",
  55.  
    txt: "", // 大于,小于,等于
  56.  
    num1Size: "default", // default small large
  57.  
    num2Size: "default"
  58.  
    }
  59.  
    },
  60.  
    methods: {
  61.  
    // 比较input1和input2值
  62.  
    compareClick() {
  63.  
    if (this.input1 > this.input2) {
  64.  
    this.txt = '大于'
  65.  
    this.num1Size = "large"
  66.  
    this.num2Size = "small"
  67.  
    } else if (this.input1 < this.input2) {
  68.  
    this.txt = '小于'
  69.  
    this.num1Size = "small"
  70.  
    this.num2Size = "large"
  71.  
    } else {
  72.  
    this.txt = '等于'
  73.  
    this.num1Size = "default"
  74.  
    this.num2Size = "default"
  75.  
    }
  76.  
    },
  77.  
    // 当input1或者input2获取焦点就清空txt
  78.  
    handleFocus() {
  79.  
    this.txt = ''
  80.  
    }
  81.  
    }
  82.  
    })
  83.  
    </script>
  84.  
    </body>
  85.  
    </html>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhcgkbia
系列文章
更多 icon
同类精品
更多 icon
继续加载