<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>键盘修饰符 todolist</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div class="app">
<!-- <input type="text" @keydown="submit($event)" placeholder="请留言"> -->
<input type="text" @keydown.enter="submit($event)" placeholder="请留言">
<ul>
<li v-for="(item, index) of list" :key="index">{{item}}</li>
</ul>
</div>
<script>
const app = Vue.createApp({
data() {
return {
// 留言列表
list: []
}
},
methods: {
submit(e) {
console.log(e.key)
// if (e.key === 'Enter') {
// // 将用户留言添加到List中,应该从数组的头部追加
this.list.unshift(e.target.value)
// // 清空输入框
e.target.value = ''
// // 阻止默认行为
// e.preventDefault()
// }
}
},
}).mount('.app')
</script>
</body>
</html>
最后修改:2025 年 04 月 08 日
© 允许规范转载
此处评论已关闭