如何禁止Nuxt.js中number类型input输入框的滚轮/滚动事件?
解决方案
有3种常用实现方式,可根据你的场景选择:
方案1:Vue项目直接加事件修饰符(适配你当前的代码,推荐)
直接在你的input标签上添加@wheel.prevent即可阻止滚轮事件的默认行为,不会影响输入框的其他正常功能:
<input type="number" class="form-control" id="phone" v-model="phone" placeholder="Mobile No" name="phone" @wheel.prevent />
方案2:原生JS实现
如果你需要脱离Vue框架实现,可通过监听滚轮事件阻止默认行为:
const phoneInput = document.querySelector('#phone') phoneInput.addEventListener('wheel', function(e) { e.preventDefault() })
方案3:适配手机号场景的优化方案
因为你当前的输入框是用于填写手机号,更推荐直接将type属性改为tel,本身就不存在滚轮修改数值的问题,同时移动端访问时会自动弹出数字键盘,更适配手机号输入场景:
<input type="tel" class="form-control" id="phone" v-model="phone" placeholder="Mobile No" name="phone" />
内容的提问来源于stack exchange,提问作者Kallol Ray
相关产品推荐
相关产品推荐

