vue快速入门(十六)事件修饰符

注释很详细,直接上代码

上一篇

新增内容

  1. 事件修饰符之阻止冒泡
  2. 事件修饰符之阻止默认行为

源码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <!-- 挂载点 -->
  <div id="root">
    <!-- @click.stop 阻止冒泡-->
    <!-- 1.未阻止冒泡时:点击子容器时显示“子容器被点击,父容器被点击” -->
    <div style="height: 200px; width: 200px; background-color: aqua;" @click="click_1">
      <div style="height: 100px; width: 100px; background-color: blue;" @click="click_2"></div>
    </div>

    <!-- 2.阻止冒泡时:点击子容器只输出“子容器被点击” -->
    <div style="height: 200px; width: 200px; background-color: red;" @click="click_3">
      <div style="height: 100px; width: 100px; background-color: blue;" @click.stop="click_4"></div>
    </div>

    <!-- 阻止默认行为:此时是不会跳转链接的-->
    <a @click.prevent="click_5" href="https://www.baidu.com">链接</a>
  </div>

    <!-- 导入vue的js代码:不会下载的看专栏第一篇 -->
    <script src="./lib/vue2.js"></script>

    <script>
      const app = new Vue({// Vue实例
        el: '#root',// 挂载点
        data: {// 数据

        },
        methods: {// 方法
          click_1() {
            console.log('父容器被点击!')
          },
          click_2() {
            console.log('子容器被点击!')
          },
          click_3() {
            console.log('父容器被点击!')
          },
          click_4() {
            console.log('子容器被点击!')
          },
          click_5() {
            console.log('a标签被点击!')
          }
        }
      })
    </script>
</body>

</html>

效果演示

在这里插入图片描述

下一篇

  • 12
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码对我眨眼睛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值