js add方法_[前端]分别原生JS和Vue实现计数器功能

题目

用vue实现计数器功能,其中vue实现的代码由黑马程序员vue教程给出,这里对其CSS代码进行了注释,并且用原生JS重新实现了一次。

效果

不能大于10

cada8d3706e2ff3243344fb1dbe0d88e.png

不能小于0

14076f0e9025913e7917d89367b32ce6.png

vue实现

实现步骤

1.确定el挂载点(id = "app"的div)

2.data中定义数据(num,min,max)

3.methods添加两个方法sub(-)和add(+)

4.使用v-text将num设置给span标签

5.使用v-on分别将sub和add绑定给-和+

6.设置num的范围,即min = 0,max=10

<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>计数器title>    <link rel="stylesheet" href="./css/index.css">  head>  <body>    <div id="app">            <div class="input-num">        <button @click="sub">          -        button>        <span>{{ num }}span>                <button @click="add">          +        button>      div>      <img      src="http://www.itheima.com/images/logo.png"      alt=""    />    div>  body>html><script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script><script>  // 创建Vue实例  var app = new Vue({    el: "#app",    data: {      num: 1,      min: 0,      max: 10    },    methods: {      sub() {        if (this.num > this.min) {          this.num--;        } else {          alert("不能小于0");        }      },      add() {        if (this.num < this.max) {          this.num++;        } else {          alert("不能大于10");        }      }    }  });script>

原生JS实现

实现步骤

1.设定数据范围

    var max = 10;    var min = 0;

2.找出要操作的对象

    var button_all = document.querySelectorAll("button");    var span_c = document.querySelector("span");    var button_mul = button_all[0];    var button_add = button_all[1];

3.为两个按钮绑定事件,并使用innerText更新span里面的内容

    button_mul.onclick = function(){      if(parseInt(span_c.innerText)>0){        console.log("点我了")        span_c.innerText = span_c.innerText - 1      }else{        alert("不能小于0");      }       }    button_add.onclick = function(){      if(parseInt(span_c.innerText)<10){      console.log("点我了+")      span_c.innerText = parseInt(span_c.innerText)+1      }else{        alert("不能大于10");      }          }

完整代码

<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>计数器title>  <link rel="stylesheet" href="./css/index.css" />head><body>    <div id="app">        <div class="input-num">      <button >        -      button>      <span>0span>      <button >        +      button>    div>    <img src="http://www.itheima.com/images/logo.png" alt="" />  div>    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>    <script>    var max = 10;    var min = 0;    var button_all = document.querySelectorAll("button");    var span_c = document.querySelector("span");    var button_mul = button_all[0];    var button_add = button_all[1];    button_mul.onclick = function(){      if(parseInt(span_c.innerText)>0){        console.log("点我了")        span_c.innerText = span_c.innerText - 1      }else{        alert("不能小于0");      }       }    button_add.onclick = function(){      if(parseInt(span_c.innerText)<10){      console.log("点我了+")      span_c.innerText = parseInt(span_c.innerText)+1      }else{        alert("不能大于10");      }          }script>body>html>

CSS代码解释

在button和span中不同位置使用flex: 1会有不用效果,在button中使用,button会改变自己的宽度,来和span一起占满父元素div的空间,效果如下

e8de8df3c755ac098fb3e352d6ea5fc3.png

而在span中使用,button的宽高不会变化,span的宽高会变化

88f60bbbe80398448e33ef0e84710c1e.png

body{  background-color: #f5f5f5;  /* background-color: yellow; */}#app {  /* background-color: black; */  width: 480px;  height: 80px;  margin: 200px auto;}.input-num {  margin-top:20px;  height: 100%;  display: flex;  border-radius: 10px; /*让边角变圆*/  overflow: hidden;   /*overflow 属性规定当内容溢出元素框时发生的事情。*/  box-shadow: 4px 4px 4px #adadad;/*阴影*/  border: 1px solid #c7c7c7;/*宽度 样式(视线)颜色*/  background-color: #c7c7c7;/*背景颜色*/}.input-num  button{  /* flex: 1; */  width: 150px;  height: 100%;  font-size: 40px;  color: #ad2a27;  cursor: pointer;  border: none;  outline: none;  background-color:rgba(0, 0, 0, 0);  /* background-color: yellow; */}.input-num span {  /* width: 164px; */  height: 100%;  font-size: 40px;  /* display: block; */  flex: 1;  text-align: center;  line-height: 80px;  font-family:auto;  /* background-color: red; */  background-color: white;}img{  float: right;  margin-top: 50px;  /* background-color: yellow; */}

CSDN链接:

https://blog.csdn.net/qq_44742090/article/details/109537886

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值