<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/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() { // 绑定 enter 事件到 body this.bodyListener = (e) => { if(e.keyCode === 13 && e.target === document.body) { alert('you press enter') } } document.body.addEventListener('keyup', this.bodyListener, false) }, methods: { // 绑定 enter 事件到 input 元素 show: function(ev) { if(ev.keyCode == 13) { alert('按了回车') } } } }) </script>
</body>
</html>
还没有评论,来说两句吧...