【Vue】—3:事件处理(狂神说系列)

3,事件处理

可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码。

  • 事件必有方法
  • 方法必须定义在Vue的methods对象中,v-on:事件

1,代码实例

<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-for="http://www.w3.org/1999/xhtml"
      xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--1,View层,模板-->
<div id="app">
<button v-on:click="sayHi">click Me</button>
</div>
<!--2,导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script>
    var vm=new Vue({
     
     
        //el:元素,绑定
        el:"#app",
        data:{
     
     
            message:"java"
        },
        methods:{
     
     
            sayHi:function (event) {
     
     
                alert(this.message)
            }
        }
    });
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/dearand/article/details/110469182