(Sjran)vue.js第二天学习

1.跑马灯

<!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="./lib/vue-2.4.0.js"></script>
  <style>
    [v-cloak]{
      display:none;
    }
  </style>
</head>
  <body>
    <div id="app">
      <input type="button" value="跑起来" @click="show">
      <input type="button" value="停住" @click="stop">
      <div>{{ msg }}</div>

    </div>

    <script>
      var vm = new Vue({
        el:'#app',
        data:{
          msg:'杭州师范大学',
          intervalId: null
        },
        methods:{
          show(){
            if(this.intervalId!=null) return;
            this.intervalId = setInterval(()=>{
            var start = this.msg.substring(0,1)
            var end = this.msg.substring(1)
            this.msg=end+start
            },400)


          },
          stop(){
              clearInterval(this.intervalId)
              this.intervalId=null
            }
          }
          


      })
    </script>
  </body>

</html>

2.事件修饰符

  <!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="./lib/vue-2.4.0.js"></script>
  <style>
   .inner{
     height: 150px;
     background-color: cadetblue;
   }
   .outer{
    padding:40px;
    background-color: red;
   }
  </style>
</head>
  <body>
    <div id="app">
      <div class=outer @click="outhandle">
        <div class=inner @click="divhandle">
          <input type="button" value="点我" @click.stop="btnhandle">
        </div>
      </div>
      
      <a href="http://www.baidu.com" @click.prevent="linkclick">有问题去百度</a>
      
    </div>
    <script>
      var vm = new Vue({
        el:'#app',
        data:{

        },
        methods:{
          divhandle(){
            console.log('这是触发了div的点击事件')
            
          },

          btnhandle(){
            console.log('这是触发了button的点击事件')
          },

          linkclick(){
            console.log('这是触发了超链接')
          },

          outhandle(){
            console.log('这是触发了out的点击事件')
          }

        }
      })
    </script>
  </body>
  

</html>

1.stop : 阻止冒泡
2.prevent:阻止默认事件
3.capture:捕获事件
4.self
5.once:只触发一次

v-model:<input type="text" v-model:value="msg" style="width: 100%;">
双向绑定

简易计算器:

<!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="./lib/vue-2.4.0.js"></script>
  <style>
   .inner{
     height: 150px;
     background-color: cadetblue;
   }
   .outer{
    padding:40px;
    background-color: red;
   }
  </style>
</head>
 <div id="app">
   <body>
   <input type="text" v-model="n1">
   <select v-model="cpt">
     <option value="+">+</option>
     <option value="-">-</option>
     <option value="*">*</option>
     <option value="/">/</option>
   </select>
   <input type="text" v-model="n2">
   <input type="button" value="=" @click="calc">
   <input type="text" v-model="result">

    <script>
      var vm =new Vue
      ({
        el:'#app',
        data:
        {
          n1:0,
          n2:0,
          result:0,
          cpt:'+'


        },
        methods:
        {
          calc(){
            /*switch(this.cpt){
              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;
            }*/
            var codestr='parseInt(this.n1)'+this.cpt+'parseInt(this.n2)'
            this.result=eval(codestr)
            
          }
        }
      })
    </script>
   </body>
 </div>
  

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值