点击事件传参

111 阅读1分钟
<div>
<button v-on:click = "showInfo">点我提示信息</button>
</div>
--------------
methods:{
   showInfo(){
   alert("同学你好")
}
  • 解析:当点击button元素时候,他就去找名为showInfo的函数,然后调用这个函数。
  • 这就是一个最简单的事件绑定。

深度理解

这个showInfo能不能接收参数;

  • 当点了一个按钮,然后调了一个函数,默认传了一个事件对象,
methods:{
  showInfo(event){
  console.log(event.target//`<button >点我提示信息</button>`
  alert("同学你好")
}

  • 这个target就是这个事件的目标,打印出来就是<button >点我提示信息</button>
  • 拿到这个元素之后,可以做很多事,可以去找到元素里面的文字;
console.log(event.target.innerText) //点我提示信息

image.png

image.png