input chrome浏览器自动填充黄色背景问题

chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对其赋予以下样式:

input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
  background-color: rgb(250, 255, 189); /* #FAFFBD; */
  background-image: none;
  color: rgb(0, 0, 0);
}
并且 chrome默认定义的 background-color background-image color 不能用 !important  提升其优先级,为了不影响美观,有以下解决方案,不同情况选择不同方案

方案一(对input:-webkit-autofill使用足够大的纯色内阴影来覆盖input输入框的黄色背景

  1. input:-webkit-autofill {
  2. -webkit-box-shadow: 0 0 0px 1000px white inset;
  3. border: 1px solid #CCC!important;
  4. }
方案二(设置表单属性 autocomplete="off/on" 关闭自动填充表单

<!-- 对整个表单设置 -->
<form autocomplete="off" method=".." action="..">
<!-- 或对单一元素设置 -->
<input type="text" name="textboxname" autocomplete="off">


                
在使用Vue框架的el-input组件时,浏览器自动填充可能会造成一些问题。浏览器自动填充是浏览器根据用户之前的输入记录和记住密码等信息自动填充输入框内容的功能。但是这个功能也许不符合我们的需求,所以需要解决这个问题。 解决浏览器自动填充的方法有以下几种: 1. 屏蔽自动填充:可以通过设置input标签的autocomplete属性为off来禁止自动填充。 ```html <el-input v-model="inputValue" :autocomplete="'off'" ></el-input> ``` 2. 修改input事件:通过监听input事件,手动更新数据模型中的值,这样可以覆盖浏览器自动填充的内容。 ```html <el-input v-model="inputValue" @input="handleInput" ></el-input> ``` ```javascript methods: { handleInput() { this.inputValue = this.$refs.input.value; // 获取input框的值并更新数据模型 }, }, ``` 3. 利用setTimeout延迟设置值:使用setTimeout可以使得设置值操作在下一次事件循环开始,从而避免被浏览器自动填充的值覆盖。 ```javascript mounted() { setTimeout(() => { this.inputValue = ''; }, 0); }, ``` 4. 结合JavaScript和Vue的解决方案:在mounted钩子函数中使用JavaScript的代码将input框的值清空,并将其焦点移至其他input元素。 ```javascript mounted() { let input = document.getElementById('inputId'); input.value = ''; let otherInput = document.getElementById('otherInputId'); otherInput.focus(); }, ``` 这些方法可以有效解决浏览器自动填充所带来的问题,根据需要选择适合的解决方案即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值