<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../../js/vue.js">script>
head>
<body>
<div id="app">
<h2>{{message}}h2>
<button @click="add">+button>
<button @click="sub">-button>
div>
<script>
let vm = new Vue({
el: '#app',
data: {
message: '0'
},
methods: {
add() {
return this.message++;
},
sub() {
return this.message--;
}
}
});
script>
body>
html>
<div id="app">
<button @click="btn1click">按钮一:有()button><br/><br/>
<button @click="btn1click">按钮一:无()button><br/><br/>
<button @click="btn2click('abc')">按钮二:传参button><br/><br/>
<button @click="btn2click()">按钮二:不传参,有括号button><br/><br/>
<button @click="btn2click">按钮二:不传参,无括号button><br/><br/>
<button @click="btn3click">按钮三:无括号button><br/><br/>
<button @click="btn3click('abc',$event)">按钮三:有括号button>
div>
<script>
let vm = new Vue({
el: '#app',
data: {
message: ''
},
methods: {
btn1click(){
console.log("点击了btn1");
},
btn2click(name){
console.log("btn2传入的参数值为:",name);
},
btn3click(name,event){
console.log("btn3传入的参数值为:",name,event);
}
}
});
script>
第一种情况:
<div id="app">
<div @click.stop="btnDIV">
<button @click.stop="btnButton">点我button>
<a class="a_submit" href="#" @click.prevent="aButton">点我a>
<form @submit.prevent>form>
<button @click.stop.prevent>button>
<input @keyup.stop.enter="enterInput"/>
<button @click.once="buttonOnce">button>
div>
div>
<script>
let vm = new Vue({
el: '#app',
data: {
message: ''
},
methods: {
btnButton() {
console.log("btnButton");
}, btnDIV() {
console.log("btnDIV");
}, aButton() {
$(this).submit();
},enterInput(){
console.log("enterInput");
},buttonOnce(){
console.log("buttonOnce");
}
}
});
script>
.native:是组件可以操作,未加组件不是不能实现点击等事件。
{{message}}
{{showContent}}
{{hideContent}}
使用标签:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>条件判断title>
<style>
h2{
display: inline-block;
}
style>
<script src="../../js/vue.js">script>
head>
<body>
<div id="app"><h2>您的成绩:h2>
<h2 v-if="score>=90">优秀h2>
<h2 v-else-if="score>=80">良好h2>
<h2 v-else-if="score>=70">一般h2>
<h2 v-else-if="score>=60">及格h2>
<h2 v-else>差劲h2>
div>
<script>
let vm = new Vue({
el: '#app',
data: {
score: 90
},
methods: {}
});
script>
body>
html>
<div id="app">
<h2>您的成绩:{{result}}h2>
div>
<script>
let vm = new Vue({
el: '#app',
data: {
score: 90
},
computed: {
result() {
if (this.score >= 90) {
return "优秀";
} else if (this.score >= 80) {
return "良好";
} else if (this.score >= 70) {
return "一般";
} else if (this.score >= 60) {
return "及格";
} else {
return "差劲";
}
}
}
});
script>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../../js/vue.js">script>
head>
<body>
<div id="app">
<span v-if="isUser">
<label for="username">用户账号:label>
<input type="text" placeholder="用户账号" id="username">
span>
<span v-else>
<label for="email">用户邮箱:label>
<input type="text" placeholder="用户邮箱" id="email">
span>
<button @click="isUser = !isUser">切换样式button>
div>
<script>
let vm = new Vue({
el: '#app',
.
message: '',
isUser :true
},
methods: {}
});
script>
body>
html>
原理:
解决方案:设置两个不同的key,即可解决用户输入数据不被处理的问题。
<body>
<div id="app">
<h2 v-if="isShow">你好h2>
<h2 v-show="isShow">你好h2>
div>
<script>
let vm = new Vue({
el: '#app',
data:{
isShow:true
}
});
script>
body>