Vue.js-监听事件(思维模型)

监听事件

在这里插入图片描述
在这里插入图片描述
注:修饰事件不干扰父元素(可以包括自身)

  • 部分代码:用于理解
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>练习</title>
<script src="vue.min.js"></script>
<style type="text/css">
   * {
       margin: 0 auto;
       text-align:center;
       line-height: 40px;
   }
   div {
       width: 100px;
       cursor:pointer;
   }
   #grandFather {
       background: green;
   }
   #father {
       background: blue;
   }
   #me {
       background: red;
   }#son {
        background: gray;
    }
</style>
</head>

<body>
<div id="content">
    <div id="grandFather"  v-on:click="doc">
        grandFather
        <div id="father" v-on:click="doc">
            father
            <div id="me" v-on:click="doc">
                me
                <div id="son" v-on:click="doc">
                son
            </div>
            </div>
        </div>
    </div>
 
</div>
   
<script>
    var content = new Vue({
        el: "#content",
        data: {
            id: ''
        },
        methods: {
            doc: function () {
                this.id= event.currentTarget.id;
                alert(this.id)
            }
        }
    })
</script>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>练习</title>
<script src="vue.min.js"></script>

</head>

<body>
<div id="div1">
   
  <a href="http://12306.com" @click="jump" >正常的链接 http://12306.com</a>
   
  <br>
  <a href="http://12306.com" @click.prevent="jump" >prevent jump()之后的链接 http://12306.com</a>
  <br>
  <a href="http://12306.com" @click.prevent >纯prevent之后的链接 http://12306.com</a>
   
  <br>
  <br>
  <form @submit="jump" action="http://12306.com">
    <button type="submit">正常的form</button>
  </form>
  <form @submit.prevent="jump" action="http://12306.com">
    <button type="submit">prevent jump()之后的form</button>
  </form>
  <form @submit.prevent action="http://12306.com">
    <button type="submit">纯prevent之后的form</button>
  </form>
   
</div>
   
<script>
new Vue({
	el:'#div1',
	data:{
	},
	methods:{
		jump:function(){
			alert("jump()函数被调用");
		}
	}
})
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值