今天学习内容
事件修饰符
stop 停止冒泡
prevent 阻止默认事件
capture 从最外层到里层的冒泡
self 只在当前元素才触发事件
once 事件只触发一次
v-model 表单数据双向绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
.box {
height: 200px;
width: 200px;
background-color: darkcyan;
}
</style>
</head>
<body>
<div id="app">
<!--默认情况下点击input触发事件, 会冒泡导致上一层的事件也会被触发-->
<div class="box" @click="box">
<input type="button" value="触发1" @click="btn">
</div>
<!--阻止冒泡-->
<div class="box" @click="box">
<input type="button" value="触发2" @click.stop="btn">
</div>
<!--a标签的默认事件是跳转-->
<a href="http://www.baidu.com" @click="jump">百度</a>
<!--阻止默认事件-->
<a href="http://www.baidu.com" @click.prevent="jump">baidu</a>
<!--触发事件后冒泡会一直往上触发-->
<div @click="one">
<div @click.capture="two">
<div @click="three">
<input type="button" value="冒泡1" @click="btn">
</div>
</div>
</div>
<!--如果在某个元素的事件加上capture, 则会先执行加上capture元素的事件,再进行冒泡-->
<div @click="one">
<div @click.capture="two">
<div @click="three">
<input type="button" value="冒泡2" @click="btn">
</div>
</div>
</div>
<!--self ,当前元素加上此修饰符后, 只在当前元素才触发事件-->
<div @click="one">
<div @click.self="two" style="width: 100px;height: 100px; background-color: #1b6d85">
<div @click="three">
<input type="button" value="冒泡3" @click="btn">
</div>
</div>
</div>
<!--once,事件只触发一次-->
<div @click="one">
<!--可以看到点击事件只触发了一次,接着再点也没法触发了-->
<div @click.self.once="two" style="width: 100px;height: 100px; background-color: #1b6d85">
<div @click="three">
<input type="button" value="冒泡4" @click="btn">
</div>
</div>
</div>
<!--v-model实现表单数据双向绑定-->
{{ msg }}
<!--当修改input的数据, data的数据会同步更新-->
<input type="text" v-model="msg">
</div>
<script src="lib/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: "啊哈哈哈哈哈"
},
methods: {
box: function () {
console.log("我是box")
},
btn: function () {
console.log("我是btn")
},
jump: function () {
alert("跳转")
},
one: function () {
console.log("第一层")
},
two: function () {
console.log("第二层")
},
three: function () {
console.log("第三层")
}
}
})
</script>
</body>
</html>