<button v-on:click="clickHandler"></button>
<button @click="clickHandler"></button>
submit
keyup
drag
scroll
error
abort
mouseover
mouseout
load等
<html>
<head>
<title>Vue.js Event Handling</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<button v-on:click = "displaynumbers">Click Me to Add No.</button>
<h2> Add Number 100 + 200 = {{total}}</h2>
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: "#app",
data: {
num1: 50,
num2 : 100,
total : ''
},
methods : {
displaynumbers : function(event) {
console.log(event);
return this.total = this.num1+ this.num2;
}
},
})
html, body {
margin: 5px;
padding: 0;
}
<button v-on:click = "displaynumbers">Click Me to Add No.</button>
<button @click = "displaynumbers">Click ME</button>
<html>
<head>
<title>Vue.js Event Handling</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "databinding">
<div v-bind:style = "styleobj" v-on:mouseover = "changebgcolor" v-on:mouseout = "originalcolor"></div>
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#databinding',
data: {
num1: 100,
num2 : 200,
total : '',
styleobj : {
width:"100px",
height:"100px",
backgroundColor:"red"
}
},
methods : {
changebgcolor : function() {
this.styleobj.backgroundColor = "blue";
},
originalcolor : function() {
this.styleobj.backgroundColor = "brown";
}
},
})
<div v-bind:style = "styleobj" v-on:mouseover = "changebgcolor" v-on:mouseout = "originalcolor"></div>
changebgcolor : function() {
this.styleobj.backgroundColor = "blue";
}
originalcolor : function() {
this.styleobj.backgroundColor = "brown";
.once
.prevent
.stop
.capture
.self
.passive
<button v-on:click.once = "buttonclicked">Click Once</button>
<html>
<head>
<title>Vue.js Event Handling</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "eg_2">
<button v-on:click.once = "buttonclickedonce" v-bind:style = "styleobj">Just once clickable</button>
Output:{{clicknum}}
<br/><br/>
<button v-on:click = "buttonclicked" v-bind:style = "styleobj">Click Me anytime</button>
Output:{{clicknum1}}
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#eg_2',
data: {
clicknum : 0,
clicknum1 :0,
styleobj: {
backgroundColor: '#2196F3!important',
cursor: 'pointer',
padding: '8px 16px',
verticalAlign: 'middle',
}
},
methods : {
buttonclickedonce : function() {
this.clicknum++;
},
buttonclicked : function() {
this.clicknum1++;
}
}
})
<button v-on:click.once = "buttonclickedonce" v-bind:style = "styleobj">Just once clickable</button>
<button v-on:click = "buttonclicked" v-bind:style = "styleobj">Click Me anytime</button>
buttonclickedonce : function() {
this.clicknum++;
},
buttonclicked : function() {
this.clicknum1++;
}
<a href = "An specific url" v-on:click.prevent = "clickme">Click Me</a>
<html>
<head>
<title>Vue.js Event Handling</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "eg_3">
<a href = "http://www.google.com" v-on:click = "clickme" target = "_blank" v-bind:style = "styleobj">Click Me</a>
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#eg_3',
data: {
clicknum : 0,
clicknum1 :0,
styleobj: {
color: '#4CAF50',
marginLeft: '20px',
fontSize: '30px'
}
},
methods : {
clickme : function() {
alert("Anchor tag is clicked");
}
}
})
<a href = "http://www.google.com" v-on:click.prevent = "clickme" target = "_blank" v-bind:style = "styleobj">Click Me</a>
<html>
<head>
<title>Vue.js Event Handling</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "eg_3">
<a href = "http://www.google.com" v-on:click.prevent = "clickme" target = "_blank" v-bind:style = "styleobj">Click Me</a>
</div>
<script src="index.js"></script>
</body>
</html>
<input type = "text" v-on: event_name.key_name= "showinputvalue"/>
<input type = "text" v-on:keyup.enter = "showinputvalue"/>
<input type = "text" v-on.keyup.ctrl.enter= "showinputvalue"/>
<html>
<head>
<title>Vue.js Event Handling</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "key_1">
<input type = "text" v-on:keyup.enter = "showinputvalue" v-bind:style = "styleobj" placeholder = "Type your name here.."/>
<h3> {{name}}</h3>
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#key_1',
data: {
name:'',
styleobj: {
width: "100%",
padding: "12px 20px",
margin: "8px 0",
boxSizing: "border-box"
}
},
methods : {
showinputvalue : function(event) {
this.name=event.target.value;
}
}
})
.enter
.tab
.delete(它同时读取" Delete"和" Backspace"键)
.esc
.space
.up
.down
.left
.right
<html>
<head>
<title>Vue.js Event Handling</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "eg_2">
<div id = "custom-event-example">
<p style = "font-size:25px;">Language displayed : <b>{{ languageclicked }}</b></p>
<button-counter
v-for = "(item, index) in languages"
v-bind:item = "item"
v-bind:index = "index"
v-on:showlanguage = "languagedisp">
</button-counter>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
vue.component('button-counter', {
template: '<button v-on:click = "displayLanguage(item)"><span style = "font-size:25px;">{{ item }}</span></button>',
data: function () {
return {
counter: 0
}
},
props:['item'],
methods: {
displayLanguage: function (lng) {
console.log(lng);
this.$emit('showlanguage', lng);
}
},
});
var vm = new Vue({
el: '#eg_2',
data: {
languageclicked: "",
languages : ["Java", "C", "C++", "Python", "Javascript", "Angular", "Data Structure", "jQuery"]
},
methods: {
languagedisp: function (a) {
this.languageclicked = a;
}
}
})
<button-counter
v-for = "(item, index) in languages"
v-bind:item = "item"
v-bind:index = "index"
v-on:showlanguage = "languagedisp">
</button-counter>
v-bind:item = "item"
v-bind:index = "index"
vue.component('button-counter', {
template: '<button v-on:click = "displayLanguage(item)"><span style = "font-size:25px;">{{ item }}</span></button>',
data: function () {
return {
counter: 0
}
},
props:['item'],
methods: {
displayLanguage: function (lng) {
console.log(lng);
this.$emit('showlanguage', lng);
}
},
});
template: '<button v-on:click = "displayLanguage(item)"><span style = "font-size:25px;">{{ item }}</span></button>',
var vm = new Vue({
el: '#databinding',
data: {
languageclicked: "",
languages : ["Java", "C", "C++", "Python", "Javascript", "Angular", "Data Structure", "jQuery"]
},
methods: {
languagedisp: function (a) {
this.languageclicked = a;
}
}
})