Vue-8、Vue事件处理

1、点击事件

<!DOCTYPE html>
<html lang="en" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml"
      xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>事件处理</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="root">
    <h1 >学校名称,{
   {
   name}}</h1>
    <button v-on:click="showInfo">点我提示信息</button>
    
</div>
<hr>
<script type="text/javascript">
    Vue.config.productionTip = false; //阻止Vue启动时产生生产提示。
    //创建Vue实例
    const vm = new Vue({
   
        el:'#root',
        data:{
   
            name:'尚硅谷',
            address:'四川'
        },
        methods:{
   
            showInfo(event){
   
                console.log(event.target.innerText);
                console.log(this);//此处this是vm
                alert('同学你好');
            }
        }
    });
</script>
</body>
</html>

简写

  <button v-on:click="showInfo">点我提示信息</button>
    <button @click="showInfo">点我提示信息</button>

方法传入参数

<!DOCTYPE html>
<html lang="en" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml"
      xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>事件处理</title>
    <!--引入vue-->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="root">
    <h1 >学校名称,{
   {
   name}}</h1>
    <button v-on:click="showInfo">点我提示信息</button>
    <button @click="showInfo2(66,$event)">点我提示信息2</button>
</div>
<hr>
<script type="text/javascript">
    Vue.config.productionTip = false; //阻止Vue启动时产生生产提示。
    //创建Vue实例
    const vm = new Vue({
   
        el:'#root',
        data:{
   
            name:'尚硅谷',
            address:'四川'
        },
        methods:{
   
            showInfo(event){
   
                console.log(event.target.innerText);
                console.log(this);//此处this是vm
                alert('同学你好1');
            },

            showInfo2(number,event){
   
                console.log(event.target.innerText);
                console.log(number);
                console.log(this);//此处this是vm
                alert('同学你好2');
            }
        }
    });
</script>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

相关推荐

  1. Vue - 事件处理详解

    2024-01-09 11:06:02       39 阅读
  2. vue3--事件处理

    2024-01-09 11:06:02       19 阅读
  3. vue 事件处理

    2024-01-09 11:06:02       22 阅读
  4. Vue2事件处理

    2024-01-09 11:06:02       12 阅读
  5. 前端vue2学习(事件处理)总结

    2024-01-09 11:06:02       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-09 11:06:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-09 11:06:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-09 11:06:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-09 11:06:02       20 阅读

热门阅读

  1. SQL-DML小结

    2024-01-09 11:06:02       36 阅读
  2. Bluez交叉编译

    2024-01-09 11:06:02       41 阅读
  3. C++ STL中vector的模拟实现

    2024-01-09 11:06:02       36 阅读
  4. 安卓adb

    2024-01-09 11:06:02       33 阅读
  5. 逐步递进地手写一个Promise

    2024-01-09 11:06:02       31 阅读
  6. 探索 GitHub:高效使用技巧与实例分享

    2024-01-09 11:06:02       40 阅读
  7. git常用指令及应用案例

    2024-01-09 11:06:02       43 阅读
  8. 程序员必备的面试技巧

    2024-01-09 11:06:02       36 阅读