文章目录



本地应用

vue之v-text、v-html及v-on标签基本使用_v-on基础

1. v-text:设置标签的文本值

vue之v-text、v-html及v-on标签基本使用_vue_02

<body>
<div id="app">
<h2>{{ message }} xdr630</h2>
<h2 v-text="message">xdr630</h2>
<h2 v-text="info">xdr630</h2>
</div>

<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello,vue',
info: "你好,vue"
}
})
</script>
</body>

vue之v-text、v-html及v-on标签基本使用_v-html_03


  • 可以看出 v-text 不能显示出后面的内容,,​​v-text​​​会覆盖元素中原来的内容,但是 插值表达式 ​​({{ }})​​只会替换自己的这个占位符,不会把 整个元素 的内容给清空
  • 同时v-text 表达式也支持字符串的拼接,如:

<h2>{{ message+'!!' }} xdr630</h2>
<h2 v-text="message+'!!'">xdr630</h2>
<h2 v-text="info+'!!'">xdr630</h2>

vue之v-text、v-html及v-on标签基本使用_vue_04

2. v-html:设置标签的innerHTML

vue之v-text、v-html及v-on标签基本使用_vue_05

案例:v-text和v-html输入内容比较

<body>
<div id="app">
<p v-html="content"></p>
<p v-text="content"></p>
</div>

<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
// content: "你好",
content: "<a href='javascript:void(0)' target='_blank'>兮动人</a>",
}
})
</script>
</body>

vue之v-text、v-html及v-on标签基本使用_v-text_06

3. v-on基础:为元素绑定事件

vue之v-text、v-html及v-on标签基本使用_vue_07

<body>
<div id="app">
<input type="button" value="v-on指令" v-on:click="doIt">
<input type="button" value="v-on简写" @click="doIt">
<input type="button" value="双击事件" @dblclick="doIt">
<h2 @click="changeFood">{{ food }}</h2>
</div>

<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
food: "番茄炒鸡蛋"
},
methods: {
doIt: function () {
alert("做It");
},
changeFood: function () {
console.log(this.food)
this.food+="好好吃!";
}
}
})
</script>
</body>
  • 点击food,就会增加一次
    vue之v-text、v-html及v-on标签基本使用_v-text_08