<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<title>title>
head>
<body>
<div id="app">
<input type="text" placeholder="请输入" @keyup.13="show($event)" />
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
name: 'Vue.js'
},
mounted: function() {
this.bodyListener = (e) => {
if(e.keyCode === 13 && e.target === document.body) {
alert('you press enter')
}
}
document.body.addEventListener('keyup', this.bodyListener, false)
},
methods: {
show: function(ev) {
if(ev.keyCode == 13) {
alert('按了回车')
}
}
}
})
script>
body>
html>