「Vue.js」v-on:click.enterでenterキーイベントを取得する
環境
Vue.js 3.0.0
Google Chrome 98.0.4758.102
書式
<button v-on:click.enter="イベント名">検証</button>
methods: {
イベント名: function () {
処理コード
}
}
「v-on:click.enter」を使用して、クリックイベント時にenterキーイベントを取得します。
使用例
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>enterキーイベントサンプル</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app" >
<h2 >回数:{{num}}</h2>
<button v-on:click.enter="kdf">検証</button>
</div>
<script>
const cft = {
data() {
return {
num: 0
}
},
methods: {
kdf: function () {
this.num++
}
}
}
Vue.createApp(cft).mount('#app')
</script>
</body>
</html>
結果
「検証」ボタンをクリックした後にenterキーを押下した際に、変数が増やして表示します。