[转]Vue基础语法之@click、时间修饰符@click.stop与@click.prevent、按键修饰符(如@keyup.enter)

1、绑定监听@click:(以监听click为例,其他如keyup,用法类似)
v-on:click="fun"
@click="fun"
@click="fun(参数)"

  <button @click="test1">test1</button>
  <button @click="test2('abc')">test2</button>
  <button @click="test3('abcd', $event)">test3</button>
 
  methods: {
      test1(eve) {//test1函数没有参数,默认传递 $event 
        alert(eve.target.innerHTML)  //test1
      },
      test2 (msg) { //test1函数有参数,传递该参数
        alert(msg)  // abc
      },
      test3 (msg, event) { //有参数,如果想获取到enevt,则函数中需要写 $event
        alert(msg+'---'+event.target.textContent)  // abcd---test3
      }
   }

 

2、@click.stop与@click.prevent

@click.stop 阻止事件冒泡

@click.prevent 阻止事件的默认行为,

<a href="http://www.baidu.com" @click.prevent="test4">百度一下</a>   //阻止a标签跳转,仅执行函数test4

<form  action="/xxx"   @submit.prevent="test5">   //阻止表单提交,仅执行函数test5

         <input type="submit" value="注册">
</form>

 

3、按键修饰符

@keyup.enter

//按下enter时,执行方法test7

<input type="text" @keyup.enter="test7">

methods: {
      test7 (event) {
        console.log(event.keyCode)
        alert(event.target.value)
      }

}

 

From: https://blog.csdn.net/wangchaohpu/article/details/84575144

 

posted @ 2021-07-20 10:27  ec04  阅读(211)  评论(0编辑  收藏  举报