监听事件
注:修饰事件不干扰父元素(可以包括自身)
- 部分代码:用于理解
<!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>