获取块元素,不用先获取Dom节点,然后在获取节点元素
点击弹出div元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="./vue.js"></script>
</head>
<!-- 获取div,操作css -->
<body>
<div id="root">
<div ref='rref' @click="handle">pig one</div>
</div>
<script>
var vm = new Vue({
el:"#root",
methods:{
handle:function(){
alert(this.$refs.rref.innerHTML)
}
}
})
</script>
</body>
</html>
点击数字,累加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue this.$refs</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
<counter ref="one" @change="handlechange"></counter>
<counter ref="two" @change="handlechange"></counter>
<div>{{total}}</div>
</div>
<script>
Vue.component('counter', {
template: '<div @click="handle">{{num}}</div>',
data: function() {
return { num: 0 }
},
methods: {
handle: function() {
this.num++
this.$emit('change')
}
}
})
var vm = new Vue({
el: "#root",
data:{
total:0
},
methods:{
handlechange:function(){
this.total = this.$refs.one.num +this.$refs.two.num
/*console.log(this.$refs.one.num+"---"+this.$refs.two.num)*/
}
}
})
</script>
</body>
</html>